Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ActionBar自定义布局样式_Android_Xml_Layout_Android Actionbar - Fatal编程技术网

Android ActionBar自定义布局样式

Android ActionBar自定义布局样式,android,xml,layout,android-actionbar,Android,Xml,Layout,Android Actionbar,我刚从IOS开始android开发,又碰到了一个问题。 经过一天的尝试,我决定我将询问堆栈溢出的人 所以我的问题是: 我有一个应用程序,在行动栏(默认无夏洛克)我想要1个标志和2行文字。 通过互联网,我找到了动作栏的setCustomView。 自定义xml已加载,但我无法获得正确的布局 因此,我首先设置操作栏: private void setupActionBar() { ActionBar actionBar = getSupportActionBar(); actionB

我刚从IOS开始android开发,又碰到了一个问题。 经过一天的尝试,我决定我将询问堆栈溢出的人

所以我的问题是:

我有一个应用程序,在行动栏(默认无夏洛克)我想要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>

但结果却不是我想要的:


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

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

<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>

您还可以使用LinearLayout作为根,添加android:weightSum并在三个子视图上指定布局权重

您没有得到结果,因为:

你需要:

  • 如果您使用AppCompat主题
  • 添加到您的活动布局中
  • 在代码中,例如在onCreate()中,设置一个工具栏来替换操作栏

  • 我爱你,我已经做了一天了。现在我可以继续了。谢谢谢谢这很有用。如果需要不同的布局,请始终使用RelativeLayout作为根!绝对的疯子,截屏了密码。谢谢你的帮助