安卓-为什么自定义ActionBar不填满宽度

4
这是我的自定义布局xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:adjustViewBounds="true" 
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/weather"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/weather" />

    <ImageView
        android:id="@+id/like"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:src="@drawable/like" />

    <ImageView
        android:id="@+id/gps2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:src="@drawable/gps2" />

    <ImageView
        android:id="@+id/gps1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:src="@drawable/gps1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical|right"
        android:textSize="14sp"
        android:layout_weight="1"
        android:textColor="#fff"
        android:text="TextView" />

</LinearLayout>

这是我的ActionBar Java代码:

    ActionBar actionBar = getSupportActionBar();
        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mCustomView = layoutInflater.inflate(R.layout.actionbar, null);
actionBar.setCustomView(mCustomView);
        actionBar.setDisplayShowCustomEnabled(true);
        getSupportActionBar().setDisplayShowCustomEnabled(true);
         actionBar.setDisplayShowTitleEnabled(false);
         actionBar.setDisplayHomeAsUpEnabled(true);
         actionBar.setDisplayUseLogoEnabled(false);
         actionBar.setDisplayShowHomeEnabled(false);
         actionBar.setHomeButtonEnabled(true);
         actionBar.setIcon(null);

这是清单文件中的活动主题:

android:theme="@style/Theme.AppCompat.Light" 

这是结果:
你可以看到,黑色布局没有填满宽度,我不知道为什么。如何使自定义 ActionBar 填充屏幕宽度?

你是否启用菜单? - Spring Breaker
android:adjustViewBounds移除它,这样会起作用吗? - Boldijar Paul
@Paul 我已经将其删除,但没有任何变化。 - mahdi yamani
@SpringBreaker 我的活动中没有关于创建菜单的内容,我的意思是我没有onCreateOptionsMenu。 - mahdi yamani
@mahdiyamani 你是如何解决的? - Erum
3个回答

2

在你的ActionBar Java代码中,替换

actionBar.setCustomView(mCustomView);

使用

actionBar.setCustomView(mCustomView, new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

希望这能帮到你!

0

只需将android:setcontentstartandroid:setcontentend设置为0dp即可。


0

尝试一下

android:layout_width="fill_parent"

并删除

android:adjustViewBounds="true"

我希望它能够正常工作


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接