【问题标题】:Application wide background color for LinearLayout with Material design使用 Material Design 的 LinearLayout 的应用范围背景颜色
【发布时间】:2016-01-03 06:45:57
【问题描述】:

我的 android 应用程序中有一个如下所示的布局,没有什么花哨的,只有三个水平放置的按钮。但是当使用 Material 设计风格时,此 LinearLayout 的默认背景颜色为黑色。

在使用 Material Theme 时,有没有办法控制所有线性布局的背景颜色?我查看了themes_matrial.xml,没有看到设置通用背景颜色的部分。我是否需要在我的 xml 文件中为所有 LinearLayout 手动设置它?

我的 styles.xml 看起来像这样:

<resources>
    <style name="AppTheme" parent="AppTheme.Base">
    </style>
    <style name="AppTheme.Base" parent="@android:style/Theme.Material.Light">
    </style>
</resources>

我的LinearLayout如下:

    <LinearLayout
        android:id="@+id/action_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/mostfav"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="mostFav"
            android:text="Most Fav" />

        <Button
            android:id="@+id/sortDateButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sortByDate"
            android:text="@string/sortdate" />

        <Button
            android:id="@+id/sortTextButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sortByText"
            android:text="@string/sorttext" />

        <TextView
            android:id="@+id/tweetCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:textColor="@color/colorPrimaryText" />
    </LinearLayout>

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    AFAIK,你不能通过使用 style.xml 来做到这一点,但你可以通过创建自定义 LinearLayout 来做到这一点。

    public class MyLinearLayout extends LinearLayout {
        public MyLinearLayout(Context context) {
            super(context);
            this.setBackgroundColor(Color.RED);
        }
    
        public MyLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.setBackgroundColor(Color.RED);
        }
    
        public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.setBackgroundColor(Color.RED);
        }
    
        public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
            this.setBackgroundColor(Color.RED);
        }
    }
    

    在您的 xml 文件中,

    <com.yourpackage.name.MyLinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"></com.yourpackage.name.MyLinearLayout>
    

    希望这会有所帮助:)

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 2016-11-03
      • 2015-02-06
      相关资源
      最近更新 更多