Android 如何在操作栏上放置选项卡,与向上按钮和菜单处于同一级别
我的应用程序有两个选项卡,我想像这样把它放在操作栏上 但是,与下面的向上按钮和菜单相比,这些选项卡始终在较低级别上分开: 我不使用: android:uiOptions=“splitActionBarWhenNarrow” 我如何才能像第一张图片那样实现我的actionbar?Android 如何在操作栏上放置选项卡,与向上按钮和菜单处于同一级别,android,android-actionbar,Android,Android Actionbar,我的应用程序有两个选项卡,我想像这样把它放在操作栏上 但是,与下面的向上按钮和菜单相比,这些选项卡始终在较低级别上分开: 我不使用: android:uiOptions=“splitActionBarWhenNarrow” 我如何才能像第一张图片那样实现我的actionbar? 非常感谢您的建议 我建议为您的操作栏创建一个自定义布局,它将有两个按钮或图像视图,看起来像选项卡,并在片段onClick()之间切换。只要有一个弹出菜单就可以显示其余的菜单项 我已经完成了布局和示例活动实施: 布局文
非常感谢您的建议 我建议为您的操作栏创建一个自定义布局,它将有两个按钮或图像视图,看起来像选项卡,并在片段onClick()之间切换。只要有一个弹出菜单就可以显示其余的菜单项 我已经完成了布局和示例活动实施: 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:showDividers="middle"
android:divider="@drawable/action_bar_divider"
android:dividerPadding="10dp"
android:weightSum="7">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:clickable="true"
android:onClick="imagesViewClick">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="IMAGES"
android:textColor="@android:color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<View
android:id="@+id/images_selected_view"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_alignParentBottom="true"
android:background="#427fed"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_gravity="center_vertical"
android:orientation="vertical"
android:clickable="true"
android:onClick="videosViewClick">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="VIDEOS"
android:textColor="@android:color/white"
android:gravity="center"
android:layout_centerInParent="true"/>
<View
android:id="@+id/videos_selected_view"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_alignParentBottom="true"
android:background="#427fed"
android:visibility="gone"/>
</RelativeLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="@drawable/ic_action_overflow"
android:layout_gravity="center_vertical"
android:layout_weight="1"/>
</LinearLayout>
}
您只需实现弹出菜单。我在这里分享我的最终代码,希望这对其他人有所帮助: MainActivity.java
package com.example.testfragments;
@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements OnClickListener{
public static String FIRST_TAB_TAG = "first";
public static String SECOND_TAB_TAG = "second";
private Button imageTabButton;
private Button videosTabButton;
private Fragment imageFragment;
private Fragment videoFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
imageFragment = Fragment.instantiate(this, FirstFragment.class.getName());
getSupportFragmentManager().beginTransaction()
.add(R.id.container, imageFragment)
.commit();
}
setupTabs();
}
@Override public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
imageTabButton = (Button)findViewById(R.id.imagesTabBtn);
imageTabButton.setOnClickListener(this);
imageTabButton.setSelected(true);
videosTabButton = (Button)findViewById(R.id.videosTabBtn);
videosTabButton.setOnClickListener(this);
return true;
}
private void setupTabs() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setLogo(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.custom_actionbar_layout);
}
/**
* A placeholder fragment to explore images
*/
public static class FirstFragment extends Fragment {
public FirstFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attachment_galllery_images,
container, false);
return rootView;
}
}
/**
* A placeholder fragment to explore videos
**/
public static class SecondFragment extends Fragment {
public SecondFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_attachment_galllery_videos,
container, false);
return rootView;
}
}
@Override
public void onClick(View v) {
int viewId = v.getId();
FragmentTransaction sft;
switch (viewId) {
case R.id.imagesTabBtn:
// getSupportFragmentManager().beginTransaction()
// .replace(R.id.container, new FirstFragment())
// .commit();
if(!imageTabButton.isSelected()){
sft = this.getSupportFragmentManager().beginTransaction();
// Check if the fragment is already initialized
if (imageFragment == null) {
// If not, instantiate and add it to the activity
imageFragment = Fragment.instantiate(this, FirstFragment.class.getName());
sft.add(R.id.container, imageFragment, FIRST_TAB_TAG);
} else {
// If it exists, simply attach it in order to show it
sft.attach(imageFragment);
}
if (videoFragment != null) {
// Detach the fragment, because another one is being attached
sft.detach(videoFragment);
}
sft.commit();
imageTabButton.setSelected(true);
videosTabButton.setSelected(false);
}
break;
case R.id.videosTabBtn:
// getSupportFragmentManager().beginTransaction()
// .replace(R.id.container, new SecondFragment())
// .commit();
if(!videosTabButton.isSelected()){
sft = this.getSupportFragmentManager().beginTransaction();
// Check if the fragment is already initialized
if (videoFragment == null) {
// If not, instantiate and add it to the activity
videoFragment = Fragment.instantiate(this, SecondFragment.class.getName());
sft.add(R.id.container, videoFragment, SECOND_TAB_TAG);
} else {
// If it exists, simply attach it in order to show it
sft.attach(videoFragment);
}
if (imageFragment != null) {
// Detach the fragment, because another one is being attached
sft.detach(imageFragment);
}
sft.commit();
videosTabButton.setSelected(true);
imageTabButton.setSelected(false);
}
default:
break;
}
}
}
自定义_actionbar_layout.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:orientation="horizontal"
android:divider="?android:dividerVertical"
android:showDividers="beginning|middle|end"
android:dividerPadding="10dp">
<RelativeLayout android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="@+id/imagesTabBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@drawable/abc_tab_indicator_ab_holo"
android:textColor="@color/abc_search_url_text_normal"
android:text="IMAGES" />
</RelativeLayout>
<RelativeLayout android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<Button
android:id="@+id/videosTabBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@drawable/abc_tab_indicator_ab_holo"
android:text="VIDEOS" />
</RelativeLayout>
</LinearLayout>
顺便说一下,这是我在支持库中找到的abc_tab_indicator_ab_holo的xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/abc_tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/abc_list_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/abc_tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abc_list_pressed_holo_dark" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/abc_tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abc_tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/abc_tab_selected_pressed_holo" />
</selector>
听起来不错。然而,我不确定我们是否可以通过onClick方法更改片段。你能给我一个建议或链接吗?对不起,我是说在按钮或ImageView的onClick()方法中,你可以从一个片段移动到另一个片段。或者干脆换个角度。谢谢你的建议,我明白你的意思了。有一件事我仍然感到困惑:我应该使用哪个视图来指示具有onSelected方法的选项卡?我认为onClick()可能还不够!
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/abc_tab_selected_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/abc_list_focused_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/abc_tab_selected_focused_holo" />
<!-- Pressed -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abc_list_pressed_holo_dark" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/abc_tab_selected_pressed_holo" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/abc_tab_unselected_pressed_holo" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/abc_tab_selected_pressed_holo" />
</selector>