我已经根据您的解决方案完成了一个解决方案,包括顶部的自定义布局+底部的自定义布局+标题/副标题+主页图标。这是代码:
public class SplitABActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getActionBar();
ab.setTitle("Action Bar Title");
ab.setSubtitle("Action Bar Subtitle");
ab.setDisplayShowHomeEnabled(true);
LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.ab_custom_layout, null);
ab.setCustomView(layout);
ab.setSplitBackgroundDrawable(new ColorDrawable(Color.BLUE));
ab.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_including_layout, menu);
RelativeLayout layout = (RelativeLayout) menu.findItem(R.id.layout_item).getActionView();
View v = getLayoutInflater().inflate(R.layout.ab_bottom_layout, null);
layout.addView(v);
return super.onCreateOptionsMenu(menu);
}
}
ab_custom_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
>
<TextView android:id="@+id/element1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#fff"
android:background="#ff0000"
android:text="Element 1"/>
<TextView
android:id="@+id/element2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000"
android:background="#f0f0f0"
android:layout_toRightOf="@+id/element1"
android:text="Element 2"/>
</RelativeLayout>
menu_included_layout.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action1"
android:title="Action 1"
android:icon="@android:drawable/ic_dialog_alert"
android:showAsAction="ifRoom" />
<item android:id="@+id/action2"
android:title="Action 2"
android:icon="@android:drawable/ic_dialog_info"
android:showAsAction="ifRoom" />
<item
android:id="@+id/layout_item"
android:actionViewClass="android.widget.RelativeLayout"
android:showAsAction="always|withText"
android:title="Layout Item"/>
</menu>
ab_bottom_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/bottom_layout_txt_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click this button"/>
<ImageButton android:id="@+id/bottom_layout_btn_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:layout_toRightOf="@+id/bottom_layout_txt_status"
android:contentDescription="@string/app_name"/>
</RelativeLayout>
代码对我来说很完美。当我的平板电脑处于纵向时,我可以看到拆分的 ActionBar。在横向上,它的宽度足以将所有物品存放在顶部。
希望能帮助到你