Android ActionBar 自定义布局样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20972324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ActionBar custom layout styling
提问by Msmit1993
I'm just starting android development coming from IOS and again stubbled onto a problem. And after 1 day of trying i decided that i will ask the people of stack overflow.
我刚刚开始从 IOS 进行 android 开发,然后又遇到了一个问题。经过 1 天的尝试,我决定询问堆栈溢出的人。
So my problem:
所以我的问题:
I have an app and in the action bar (default no sherlock) i want 1 logo and 2 lines of text. And trough the internet i fount the setCustomView for the action bar. And the custom xml is loaded but i can't get the layout right.
我有一个应用程序,在操作栏中(默认没有 Sherlock),我想要 1 个徽标和 2 行文本。通过互联网,我为操作栏找到了 setCustomView。自定义 xml 已加载,但我无法正确布局。
So first i setup the action bar:
所以首先我设置了操作栏:
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);
}
Than the xml:
比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>
But the result is something what i'm not looking for:
但结果是我不想要的东西:
So what am i forgetting/doing wrong/or should i do?
那么我忘记/做错了什么/或者我应该做什么?
采纳答案by Fahriyal Afif
try change the root layout into relative layout
尝试将根布局更改为相对布局
<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>
回答by ceph3us
you're not getting your result because:
你没有得到你的结果,因为:
u need to:
你需要:
- if you use AppCompat theme
- 如果您使用 AppCompat 主题
- add to your activity layout
- 添加到您的活动布局
- in code eg. in onCreate() set a toolbar to replace the action bar.
- 在代码中,例如。在 onCreate() 中设置一个工具栏来替换操作栏。
回答by Clocker
You could also use LinearLayout as a root and add android:weightSum and specify a layout_weight on the three child views.
您还可以使用 LinearLayout 作为根并添加 android:weightSum 并在三个子视图上指定 layout_weight。