我有一个应用程序,带有一个包含 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 |
|________________________________|
关于android - 使用 appcompat 自定义操作栏布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330513/