簡體   English   中英

具有Material Design設計的LinearLayout的應用程序寬背景色

[英]Application wide background color for LinearLayout with Material design

我的android應用程序中有一個布局,如下所示,沒有花哨的地方,只有三個按鈕水平放置。 但是,當使用“材質”設計樣式時,此LinearLayout的默認背景顏色為黑色。

使用Material Theme時,是否可以控制所有Linerar布局的背景顏色? 我通過themes_matrial.xml查看了沒有設置通用背景色的部分。 是否需要在所有LinearLayouts的xml文件中手動設置它?

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

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>

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM