【问题标题】:Android ActionBar custom layout stylingAndroid ActionBar 自定义布局样式
【发布时间】:2014-01-25 04:14:20
【问题描述】:

我刚刚开始从 IOS 开发 android 并再次遇到问题。 经过 1 天的尝试,我决定向堆栈溢出的人询问。

所以我的问题:

我有一个应用程序,在操作栏中(默认没有 sherlock)我想要 1 个徽标和 2 行文本。 通过互联网,我为操作栏找到了 setCustomView。 并且自定义 xml 已加载,但我无法正确布局。

所以首先我设置了操作栏:

private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    LayoutParams lp1 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null); // layout which contains your button.

    actionBar.setCustomView(customNav, lp1);
}

比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:layout_gravity="fill_horizontal"
    android:orientation="horizontal"
    android:background="@color/Blue">

    <TextView
        android:id="@+id/text_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_menu"
        android:textColor="@color/White"
        android:paddingLeft="5dp"
        android:layout_gravity="left"/>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/Icon"
        android:layout_gravity="center"/>

    <TextView
        android:id="@+id/text_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_help"
        android:layout_gravity="right"
        android:textColor="@color/White"
        android:paddingRight="5dp"/>

</LinearLayout>

但结果是我不想要的:

那么我忘记/做错了什么/或者我应该做什么?

【问题讨论】:

    标签: android xml layout android-actionbar


    【解决方案1】:

    尝试将根布局改为相对布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="fill_horizontal"
    android:orientation="horizontal">
    
    <TextView
        android:text="left text"
        android:layout_toLeftOf="@+id/icon"
        android:layout_alignParentLeft="true"
        android:id="@+id/text_left"
        android:gravity="left"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"/>
    
    <ImageView
        android:layout_centerHorizontal="true"
        android:id="@+id/icon"
        android:layout_width="10dp"
        android:layout_height="wrap_content"
        android:src="@drawable/bg_sunrise"
        android:layout_gravity="center"/>
    
    <TextView
        android:text="Right txt"
        android:layout_toRightOf="@+id/icon"
        android:id="@+id/text_right"
        android:layout_width="match_parent"
        android:gravity="right"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:paddingRight="5dp"/> </RelativeLayout>
    

    【讨论】:

    • 我爱你,我用了 1 天就完成了。现在我可以继续了。谢谢
    • 谢谢,这很有用。如果您需要不同的布局,请始终使用 RelativeLayout 作为根!
    【解决方案2】:

    你没有得到你的结果,因为:

    你需要:

    1. 如果你使用 AppCompat 主题

    1. 添加到您的活动布局

    1. 在代码中,例如。在 onCreate() 中设置一个工具栏来替换操作栏。

    【讨论】:

    • 绝对疯子,截取了代码。感谢您的帮助
    【解决方案3】:

    您也可以使用 LinearLayout 作为根并添加 android:weightSum 并在三个子视图上指定 layout_weight。

    What is android:weightSum in android, and how does it work?

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多