我有一个应用程序,其中包含一个包含3个imageView的操作栏,我正在使用appcompat库,这里是代码:
private void setupActionBar() {
// Inflate a "Done/Cancel" custom action bar view.
final LayoutInflater inflater = (LayoutInflater) this
.getSupportActionBar().getThemedContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
R.layout.my_custom_actionbar, null);
customActionBarView.findViewById(R.id.title);
final ActionBar actionBar = this.getSupportActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM |
ActionBar.DISPLAY_SHOW_HOME |
ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
);
}
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="match_parent"
android:background="@color/gray" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal" >
<ImageView
android:id="@+id/facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:src="@drawable/ic_fb" />
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:src="@drawable/ic_hotels" />
<ImageView
android:id="@+id/tweetter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/ic_tweet"
/>
</FrameLayout>
</RelativeLayout>
自定义操作栏加载没有问题,但我无法获得正确的布局
我尝试在XML文件中进行一些更改,但它不起作用。那我忘记了什么?
答案 0 :(得分:3)
当我尝试你发布的代码时,我得到了这个结果:
________________________________
| |
| facebook tweeter logo |
|________________________________|
然后,我猜layout_gravity
属性是错误的。而且,似乎你左右反转。 FrameLayout
在您的情况下似乎毫无用处( - 我等待您的回答以确定我的评论)。这种布局可能达到你想要的效果:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use fill_horizontal to get the whole actionbar
And center_vertical gravity on all the child views -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:layout_gravity="fill_horizontal"
android:gravity="center_vertical" >
<!-- Use layout_alignParentRight attribute -->
<ImageView
android:id="@+id/facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fb"
android:layout_alignParentRight="true" />
<!-- Use layout_alignParentLeft attribute -->
<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_hotels"
android:layout_alignParentLeft="true" />
<!-- Use layout_toLeftOf (id) attribute -->
<ImageView
android:id="@+id/tweetter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_tweet"
android:layout_toLeftOf="@id/facebook" />
</RelativeLayout>
我得到了这个输出:
________________________________
| |
| logo tweeter facebook |
|________________________________|