android - match_parent doesn't work for custom actionbar -
i try implement custom action bar. here xml:
<?xml version="1.0" encoding="utf-8"?> <framelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/action_bar"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:text="test "/> </framelayout>
and here java code:
public class customactionbaractivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); android.support.v7.app.actionbar actionbar = getsupportactionbar(); actionbar.setdisplayshowcustomenabled(true); actionbar.setdisplayoptions(android.support.v7.app.actionbar.display_show_custom); actionbar.setdisplayhomeasupenabled(false); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayuselogoenabled(false); view customview = getlayoutinflater().inflate(r.layout.action_bar, null); actionbar.setcustomview(customview); toolbar parent = (toolbar) customview.getparent(); parent.setcontentinsetsabsolute(0, 0); } }
the result is:
here result "developer options" > "show layout bounds"
as @muhannad fakhouri in comment pointed out. needed add layout params programatically. layout params ignored if layout inflated without specifying parent. here updated code:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); android.support.v7.app.actionbar actionbar = getsupportactionbar(); actionbar.setdisplayshowcustomenabled(true); actionbar.setdisplayoptions(android.support.v7.app.actionbar.display_show_custom); actionbar.setdisplayhomeasupenabled(false); actionbar.setdisplayshowhomeenabled(false); actionbar.setdisplayuselogoenabled(false); view customview = getlayoutinflater().inflate(r.layout.action_bar, null); customview.setlayoutparams(new framelayout.layoutparams(framelayout.layoutparams.match_parent, framelayout.layoutparams.match_parent)); actionbar.setcustomview(customview); toolbar parent = (toolbar) customview.getparent(); parent.setcontentinsetsabsolute(0, 0); }
Comments
Post a Comment