操作按钮不覆盖整个工具栏 [英] Action Buttons do not cover the entire Toolbar

查看:304
本文介绍了操作按钮不覆盖整个工具栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一个工具栏显示四个操作按钮。当我与菜单充气工具栏,操作按钮都朝着一个侧面显示图片所示:

在这里输入的形象描述

我怎样才能让这些操作按钮覆盖整个工具栏?

这是code的活动:

 公共类ResultActivity扩展FragmentActivity {最终诠释TAB_NUM = 2;
私人ViewPager mViewPager;
私人PagerAdapter mPagerAdapter;
私人工具栏mResultToolbar;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_result);
    mResultToolbar =(工具栏)findViewById(R.id.result_toolbar);
    mResultToolbar.inflateMenu(R.menu.result_menu);
    mResultToolbar.setOnMenuItemClickListener(新Toolbar.OnMenuItemClickListener(){
        @覆盖
        公共布尔onMenuItemClick(菜单项项){
            INT ID = item.getItemId();
            开关(ID){
                案例R.id.camera_action:
                    意图cameraIntent =新意图(getApplicationContext(),CameraActivity.class);
                    startActivity(cameraIntent);
                    打破;
                案例R.id.gallery_action:
                    意向意图=新意图(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(意向,0);
                    打破;
                案例R.id.profile_action:
                    Toast.makeText(getApplicationContext(),档案,Toast.LENGTH_SHORT).show();
                    打破;
                案例R.id.slide_up_action:
                    Toast.makeText(getApplicationContext(),滚动条,Toast.LENGTH_SHORT).show();
                    打破;
            }
            返回true;
        }
    });
    mViewPager =(ViewPager)findViewById(R.id.result_view_pager);
    mPagerAdapter =新ResultViewPagerAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(mPagerAdapter);
    mViewPager.setPageTransformer(真,新DepthPageTransformer());
    }
}

这是XML:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_marginLeft =@扪/ activity_margin_horizo​​ntal
机器人:layout_marginRight =@扪/ activity_margin_horizo​​ntal
机器人:背景=@色/ app_background>< android.support.v4.view.ViewPager
    机器人:ID =@ + ID / result_view_pager
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID / result_toolbar
    机器人:layout_alignParentTop =真
    机器人:背景=@色/ colorAccent>    < android.support.v4.view.PagerTabStrip
        机器人:ID =@ + ID / result_page_tab_strip
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =顶
        机器人:背景=@色/ instagram_background/>< /android.support.v4.view.ViewPager>< android.support.v7.widget.Toolbar
    机器人:ID =@ ID / result_toolbar
    机器人:layout_width =match_parent
    机器人:layout_height =@扪/ toolbar_height
    机器人:layout_alignParentBottom =真
    机器人:背景=@彩色/ default_line_indicator_selected_color
    机器人:海拔=@扪/ toolbar_elevation>
< /android.support.v7.widget.Toolbar>< / RelativeLayout的>


解决方案

您可以添加的LinearLayout与参数工具栏

 <的LinearLayout机器人:layout_height =?ATTR / actionBarSize
 机器人:layout_width =match_parent
 机器人:方向=horizo​​ntaly
 机器人:weightSum =4> < / LinearLayout中>

然后按钮

 <按钮的android:layout_height =match_parent
        机器人:layout_width =0dp
        机器人:layout_weight =1>< /按钮>

和每个按钮将采取的工具栏空间的1/4。

所以总结一下,工具栏里面你将有:

 <的LinearLayout机器人:layout_height =?ATTR / actionBarSize
     机器人:layout_width =match_parent
     机器人:方向=horizo​​ntaly
     机器人:weightSum =4>
<按钮的android:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1>< /按钮>
<按钮的android:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1>< /按钮>
<按钮的android:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1>< /按钮>
<按钮的android:layout_height =match_parent
            机器人:layout_width =0dp
            机器人:layout_weight =1>< /按钮>
 < / LinearLayout中>

我写它的头,所以我没有测试code,但我希望这将是对你有所帮助。

I have used a Toolbar to display four action buttons. When I inflate the toolbar with menu, the action buttons are displayed towards one side as shown in the picture :

How can I make these action buttons cover the entire toolbar?

This is the code for the 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());
    }
}

This is the 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>

解决方案

You can add LinearLayout to toolbar with parameters

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

then for buttons

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

and every button will take 1/4 of toolbar space.

so to sum up, inside toolbar you will have:

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

I wrote it from head, so I not tested this code, but I hope it will be helpful for you.

这篇关于操作按钮不覆盖整个工具栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆