我有一个应用程序,带有一个包含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文件中进行一些更改,但没有成功。那我忘记了什么?
当我尝试按照您发布的代码进行编码时,我得到以下结果:
________________________________
| |
| 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 |
|________________________________|
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句