【问题标题】:android-Why custom actionbar doesn't fill the widthandroid-为什么自定义actionbar没有填满宽度
【发布时间】:2015-02-23 05:13:25
【问题描述】:

这是我的自定义布局 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>

这是我的操作栏 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" 

这是结果:

如您所见,黑色布局没有填满宽度,我不知道为什么。如何制作自定义操作栏来填充屏幕宽度?

【问题讨论】:

  • 您是否启用了菜单?
  • android:adjustViewBounds 去掉这个,能用吗?
  • @Paul 我删除了它,没有任何改变
  • @SpringBreaker 我的活动没有创建菜单,我的意思是我没有 onCreateOptionsMenu 。
  • @mahdiyamani 你是怎么解决的?

标签: android android-layout android-actionbar


【解决方案1】:

在你的actionbar java代码中,替换

actionBar.setCustomView(mCustomView);

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

希望对你有所帮助!

【讨论】:

    【解决方案2】:

    试试

    android:layout_width="fill_parent"
    

    并删除

    android:adjustViewBounds="true"
    

    我希望它会起作用

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-21
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 2022-11-11
        相关资源
        最近更新 更多