简体   繁体   English

布局操作栏appcompat

[英]Layout actionbar appcompat

I am using an appcompat v7 actionbar with sliding tabs and navigational drawer . 我正在使用带有sliding tabs and navigational drawerappcompat v7操作sliding tabs and navigational drawer Reference taken from here . 参考资料取自此处 As you can see in the image, the tabs are not evenly spaced in the tabViewBar . 正如您在图像中看到的那样,选项卡在tabViewBar中没有均匀分布。 I want to modify the layout of the actionbar, such that it spans out evenly to use the space. 我想修改操作栏的布局,以使其均匀分布以使用该空间。

This is how the tabBarView initializes and add the tabViews : 这就是tabBarView初始化并添加tabViews

public class TabBarView extends LinearLayout {
public TabBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.MATCH_PARENT));

        setWillNotDraw(false);

        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setAntiAlias(true);

        mStripHeight = (int) (STRIP_HEIGHT * getResources().getDisplayMetrics().density + .5f);
    }

private void addTabViewP(final int i, final String string, int pageIconResId) {
        // TODO Auto-generated method stub

        if (i == 0) {
            tabView1 = new TabView(getContext());
            tabView1.setIcon(pageIconResId);
            tabView1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(i);
                }
            });
            CheatSheet.setup(tabView1, string);
            this.addView(tabView1);
        } else if (i == 1) {
            tabView2 = new TabView(getContext());
            tabView2.setIcon(pageIconResId);
            tabView2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(i);
                }
            });
            CheatSheet.setup(tabView2, string);
            this.addView(tabView2);
        } else if (i == 2) {
            tabView3 = new TabView(getContext());
            tabView3.setIcon(pageIconResId);
            tabView3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem(i);
                }
            });
            CheatSheet.setup(tabView3, string);
            this.addView(tabView3);
        }
    }
}

Following is the tabBarView custom layout: 以下是tabBarView自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<com.sanat.tinderactionbar.tabbarview.TabBarView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/customTabBar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
</com.sanat.tinderactionbar.tabbarview.TabBarView>

Here is the styles.xml file: 这是styles.xml文件:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/blue_grey_700</item>
</style>

I tried to add <item name="weightSum">4<item> in my parent theme, but no change takes place. 我尝试在父主题中添加<item name="weightSum">4<item> ,但未进行任何更改。

Even when I modify the setLayoutParams in TabBarView to: 即使将TabBarViewsetLayoutParams修改为:

LinearLayout.LayoutParams params = new LayoutParams(0,
                    LayoutParams.MATCH_PARENT);
params.weight = 0;
setLayoutParams(params);

This doesnt work either. 这也不起作用。 Where and how should I change the layout/code to make it work? 我应该在哪里以及如何更改布局/代码以使其起作用?

So: horizontal linear layout, full screen on width, right? 所以:水平线性布局,全屏显示宽度,对不对?

Don't set weightSum on the parent. 不要在父weightSum上设置weightSum For the (4?) children views: set width=0 and weight=1 . 对于(4?)子视图:设置width=0weight=1 This gives them equal importance without having to divide by their number (they are still going to be equally divided if you add a fifth one). 这使他们具有同等的重要性,而不必除以他们的数目(如果再加上第五个,它们仍将被均分)。 In xml you'd have something like this (please excuse my random image choice from Android's resources and my choice of control (ImageView) - you might have to play with the gravity and layout_gravity if you include some other layouts): 在xml中,您将具有以下内容(请从Android资源和控件选择(ImageView)中随机选择我的图像-如果包含其他布局,则可能需要使用gravity和layout_gravity):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@android:drawable/btn_dialog"/>
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@android:drawable/btn_dialog"/>
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@android:drawable/btn_dialog"/>
    <ImageView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:src="@android:drawable/btn_dialog"/>
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM