【问题标题】:Android center custom view in actionbar操作栏中的Android中心自定义视图
【发布时间】:2013-12-27 01:38:52
【问题描述】:

尝试为我的操作栏设置自定义视图,但问题是它没有填满操作栏的整个宽度,这让我很难将对象居中。如何在我的操作栏中设置一个自定义视图来填满整个操作栏?

设置我的操作栏的代码:

actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
actionBar.setCustomView(inflater.inflate(R.layout.actionbar_layout, null));

还有我的 actionbar_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#EDEDED">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/title"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:text="Testing"/>
</RelativeLayout>

【问题讨论】:

标签: android android-custom-view android-actionbar-compat


【解决方案1】:

您必须在 actionbar_layout.xml 中为您的 RelativeLayout 设置 android:layout_width="wrap_content";这与 android:layout_gravity="center" 结合使用,可以让您的自定义视图在 ActionBar 中居中。

【讨论】:

    【解决方案2】:

    就我而言,问题是上部布局总是拉伸到内部视图的高度和重量。您可以通过设置 layout_height="match_parent" 使其工作 在文本视图中:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/actionbar_title"
            />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多