自定义操作栏不填充父级

时间:2015-07-07 09:41:10

标签: android

自定义操作栏未填充父级。

我正在使用自定义操作栏来导航抽屉。     但是我的操作栏会在操作栏的左侧留下一些空间。

我使用了这样的代码。

// to inflate custom view

            LayoutInflater inflator=   (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v=inflator.inflate(R.layout.custom_action_top_bar, null);
            getActionBar().setHomeButtonEnabled(false);
            getActionBar().setDisplayShowTitleEnabled(false);
            getActionBar().setDisplayShowCustomEnabled(true);
            getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
            getActionBar().setCustomView(v);

            View v1 = getActionBar().getCustomView();
            LayoutParams lp = (LayoutParams) v1.getLayoutParams();
            lp.width = LayoutParams.FILL_PARENT;
            v1.setLayoutParams(lp);

这是action_custom_action_top_bar文件供参考

此xml文件包含主线性布局,左右图像视图分别用于打开双面抽屉。但是增加了空间  在动作栏的左侧可以帮助我吗?

这是我用过的一些背景颜色的自定义操作栏  我已经给了动作栏和抽屉每侧的两张图像

   <LinearLayout                 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <ImageView
            android:id="@+id/IV_leftIcon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".2"
            android:background="@color/light_green"
            android:src="@drawable/header_menu_btn" />

        <TextView
            android:id="@+id/actionText"
            style="@style/action_bar_top_text_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".6"
            android:background="@color/light_green"
            android:drawableRight="@drawable/header_dropdown_arrow"
            android:gravity="center"
            android:text="Sample"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/IV_rightIcon"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".2"
            android:background="@color/light_green"
            android:src="@drawable/plus_btn" />

    </LinearLayout>

2 个答案:

答案 0 :(得分:10)

如何使动作栏填充宽度

  

更新的答案

在root linear中删除android:layout_gravity =“center”就像这样,make height wrap_content

<LinearLayout                 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal"
        android:weightSum="1.0" > 

      ......
</LinearLayout>

首选使用getSupportActionBar()作为默认活动扩展ActionBarActivity

import android.support.v7.app.ActionBar.LayoutParams;
import android.support.v7.widget.Toolbar;


public class MainActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


      getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));



    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.MATCH_PARENT);
    getSupportActionBar().setCustomView(v, layoutParams);
    Toolbar parent = (Toolbar) v.getParent();
    parent.setContentInsetsAbsolute(0, 0);

}
}
  

摘要

我理解为什么你有问题,你使用旧支持库的eclipse 那个dosenot支持ToolBar 使用appcompat-v21或更高版本

我给你的建议,使用Android工作室,因为他们停止支持eclipse

此代码100%工作,证明概念

enter image description here

答案 1 :(得分:4)

只需将这两行代码添加到工具栏xml代码中

即可
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp"

它将解决您的问题。

这是我的工作代码:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay"
    app:contentInsetStart="0dp"
    app:contentInsetEnd="0dp">
    <include layout="@layout/your_custom_toolbar_layout"/>
</android.support.v7.widget.Toolbar>