2

我使用了一个工具栏来显示四个操作按钮。当我用菜单膨胀工具栏时,操作按钮显示在一侧,如图所示:

在此处输入图像描述

如何使这些操作按钮覆盖整个工具栏?

这是 Activity 的代码:

public class ResultActivity extends FragmentActivity {

final int TAB_NUM = 2;
private ViewPager mViewPager;
private PagerAdapter mPagerAdapter;
private Toolbar mResultToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
    mResultToolbar = (Toolbar) findViewById(R.id.result_toolbar);
    mResultToolbar.inflateMenu(R.menu.result_menu);
    mResultToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            switch (id) {
                case R.id.camera_action :
                    Intent cameraIntent = new Intent(getApplicationContext(),CameraActivity.class);
                    startActivity(cameraIntent);
                    break;
                case R.id.gallery_action :
                    Intent intent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 0);
                    break;
                case R.id.profile_action :
                    Toast.makeText(getApplicationContext(),"Profile",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.slide_up_action :
                    Toast.makeText(getApplicationContext(),"Slider",Toast.LENGTH_SHORT).show();
                    break;
            }
            return true;
        }
    });
    mViewPager = (ViewPager)findViewById(R.id.result_view_pager);
    mPagerAdapter = new ResultViewPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setPageTransformer(true, new DepthPageTransformer());
    }
}

这是 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_marginRight="@dimen/activity_margin_horizontal"
android:background="@color/app_background">

<android.support.v4.view.ViewPager
    android:id="@+id/result_view_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/result_toolbar"
    android:layout_alignParentTop="true"
    android:background="@color/colorAccent">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/result_page_tab_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/instagram_background" />

</android.support.v4.view.ViewPager>

<android.support.v7.widget.Toolbar
    android:id="@id/result_toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/toolbar_height"
    android:layout_alignParentBottom="true"
    android:background="@color/default_line_indicator_selected_color"
    android:elevation="@dimen/toolbar_elevation">
</android.support.v7.widget.Toolbar>

</RelativeLayout>
4

2 回答 2

1

您可以使用参数将 LinearLayout 添加到工具栏

 <LinearLayout android:layout_height="?attr/actionBarSize"
 android:layout_width="match_parent"
 android:orientation="horizontaly"
 android:weightSum="4">  </LinearLayout>

然后是按钮

<Button android:layout_height="match_parent" 
        android:layout_width="0dp"
        android:layout_weight="1"></Button>

每个按钮将占用工具栏空间的 1/4。

总而言之,在工具栏中,您将拥有:

<LinearLayout android:layout_height="?attr/actionBarSize"
     android:layout_width="match_parent"
     android:orientation="horizontaly"
     android:weightSum="4"> 
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
<Button android:layout_height="match_parent" 
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
 </LinearLayout>

我是从头开始写的,所以我没有测试这段代码,但我希望它对你有帮助。

于 2016-02-04T10:53:15.943 回答
0

如果您只有四个菜单选项,那么您可以考虑不使用 Toolbar 。相反,您可以直接在 AppBarlayout 中使用 LinearLayout,因为它也可以容纳其他视图和工具栏。喜欢

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/app_background">

    <LinearLayout
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:orientation="horizontaly"
        android:weightSum="4">

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>

        <Button
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1"></Button>
    </LinearLayout>
    </android.support.design.widget.AppBarLayout>

我使用了与 MyWay 相同的布局。谢谢我的方式。

于 2016-02-04T11:23:08.037 回答