[英]match_parent doesn't work for custom actionbar
I try to implement a custom action bar. 我尝试实现自定义操作栏。 Here is the xml:
这是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 is the java code: 这是java代码:
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 is the result with "developer options" > "show layout bounds" 这是“开发者选项”>“显示布局边界”的结果
As @Muhannad Fakhouri in comment pointed out. 正如@Muhannad Fakhouri在评论中指出的那样。 I needed to add layout params programatically.
我需要以编程方式添加布局参数。 Layout params will be ignored if the layout inflated without specifying the parent.
如果布局膨胀而未指定父级,则将忽略布局参数。 Here is the 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.