Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android手机的选项卡中添加右端溢出菜单_Android_Android Tabs_Android Appcompat - Fatal编程技术网

如何在Android手机的选项卡中添加右端溢出菜单

如何在Android手机的选项卡中添加右端溢出菜单,android,android-tabs,android-appcompat,Android,Android Tabs,Android Appcompat,我正在制作一个android应用程序,我需要创建两个选项卡,这很简单,但我需要在同一行中添加溢出菜单。见截图 我从来没有在Android中实现过这一点,这对我来说可能更棘手 如果你知道如何在Android中实现这一点,请帮助我 我只是使用下面的代码实现了选项卡 public class PayStubActivity extends ActionBarActivity implements ActionBar.TabListener { @Override protected

我正在制作一个android应用程序,我需要创建两个
选项卡
,这很简单,但我需要在同一行中添加溢出菜单。见截图

我从来没有在Android中实现过这一点,这对我来说可能更棘手

如果你知道如何在Android中实现这一点,请帮助我

我只是使用下面的代码实现了
选项卡

public class PayStubActivity extends ActionBarActivity implements ActionBar.TabListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // requesting to set the custom title bar
        setContentView(R.layout.activity_pay_stub);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

        // adding action bar tabs to the activity
        ActionBar mActionBar = getSupportActionBar();
        Tab firstTab = mActionBar.newTab().setText(getString(R.string.paystub_current_tab)).setTabListener(this);
        Tab secondTab = mActionBar.newTab().setText(getString(R.string.paystub_all_tab)).setTabListener(this);

        // add the tabs to the action bar
        mActionBar.addTab(firstTab);
        mActionBar.addTab(secondTab);

        // set the navigation mode the Action Bar Tabs
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        // user is selected first tab
        if(tab.getPosition() == 0) {
            fragmentTransaction.replace(R.id.container, new CurrentPayStubFragment());
        } else {
            // user is selected second tab.
        }
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {}

}

提前感谢。

Gabriele Mariotti在CardView类中有一个很好的实现

CardView由一个

政府将在这方面为你提供极大的帮助


HTH

您需要为操作栏定制布局:

布局\动作\栏:

<?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>

这是你想要的吗?()@shayanpourvatan见截图。我想在两个
ActionBar
选项卡的右端添加“溢出”图标。有什么帮助吗?正如您所说,它在
操作栏中添加了“溢出”菜单,而不是在选项卡中。我希望“溢出”菜单位于选项卡的右端,当您在OnCreateOptions菜单中将项目添加到菜单时,它现在在做什么?我不明白。。你的意思是它创建了一个带有溢出的经典动作栏,并且标签在下面?如果是这样的话,我看不到任何其他的解决方案,除了自定义视图和一些创建选项卡(ViewPagerIndicator)和溢出菜单按钮的库
public class MainActivity extends ActionBarActivity{

private View imagesViewSelected;
private View videosViewSelected;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new ImagesFragment())
                .commit();
    }
}

private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowTitleEnabled(false);
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);

    ActionBar.LayoutParams lp1 = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    View customNav = LayoutInflater.from(this).inflate(R.layout.layout_action_bar, null);

    imagesViewSelected = customNav.findViewById(R.id.images_selected_view);
    videosViewSelected = customNav.findViewById(R.id.videos_selected_view);

    actionBar.setCustomView(customNav, lp1);
}


public static class ImagesFragment extends Fragment {

    public ImagesFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ((TextView)rootView.findViewById(R.id.txt_fragment_locator)).setText("Images Fragment");

        return rootView;
    }
}

public static class VideosFragment extends Fragment {

    public VideosFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        ((TextView)rootView.findViewById(R.id.txt_fragment_locator)).setText("Videos Fragment");

        return rootView;
    }
}

public void imagesViewClick(View view){
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new ImagesFragment())
            .commit();
    imagesViewSelected.setVisibility(View.VISIBLE);
    videosViewSelected.setVisibility(View.GONE);
}

public void videosViewClick(View view){
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, new VideosFragment())
            .commit();
    imagesViewSelected.setVisibility(View.GONE);
    videosViewSelected.setVisibility(View.VISIBLE);
}
}