Android ActionBar自定义布局样式
我刚从IOS开始android开发,又碰到了一个问题。 经过一天的尝试,我决定我将询问堆栈溢出的人 所以我的问题是: 我有一个应用程序,在行动栏(默认无夏洛克)我想要1个标志和2行文字。 通过互联网,我找到了动作栏的setCustomView。 自定义xml已加载,但我无法获得正确的布局 因此,我首先设置操作栏: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
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并在三个子视图上指定布局权重 您没有得到结果,因为: 你需要:
我爱你,我已经做了一天了。现在我可以继续了。谢谢谢谢这很有用。如果需要不同的布局,请始终使用RelativeLayout作为根!绝对的疯子,截屏了密码。谢谢你的帮助