如何为不同情况下的布局内的多个视图设置样式?

时间:2018-02-23 07:03:59

标签: android android-styles

案例1:拥有图像

enter image description here

案例2:没有图像

ImageView将消失,叠加阴影将不可见文本颜色将会改变。

可以使用样式吗?

不使用样式,可以调用函数并设置所需的属性,但如何使用样式来完成?

这是我的xml文件

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/eight"
        android:layout_marginBottom="@dimen/eight">
        <ImageView
            android:id="@+id/news_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/bg"
            android:scaleType="centerCrop"
            android:visibility="visible" />
        <!--android:background="?attr/selectableItemBackground"-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/map_shadow_up"
            android:padding="@dimen/four"
            android:gravity="center">

            <LinearLayout
                android:id="@+id/ln_hash_container"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/twelve"
                android:layout_marginRight="@dimen/eight"
                android:layout_weight="1">
                <TextView
                    android:id="@+id/ln_hash"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="?attr/selectableItemBackground"
                    android:textColor="@color/white"
                    android:text="#1234"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingTop="@dimen/four"
                android:paddingBottom="@dimen/four"
                android:paddingLeft="@dimen/eight"
                android:paddingRight="@dimen/eight"
                android:background="@drawable/circular_corner">
                <ImageView
                    android:layout_width="@dimen/activity_horizontal_margin"
                    android:layout_height="@dimen/activity_horizontal_margin"
                    app:srcCompat="@drawable/star"
                    android:tint="@color/colorAccent"
                    android:layout_marginRight="@dimen/four"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="4"
                    android:textColor="@color/colorAccent"/>
                <RatingBar
                    android:id="@+id/item_ln_ratingBar"
                    style="@style/Widget.AppCompat.RatingBar.Small"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/four"
                    android:layout_marginBottom="@dimen/eight"
                    android:layout_marginLeft="@dimen/eight"
                    android:layout_marginRight="@dimen/eight"
                    android:layout_weight="0"
                    android:visibility="gone"
                    android:isIndicator="true" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我使用了attrs.xml

在那里我创建了许多引用,如

<attr name="newsImage" format="reference"/>
<attr name="overlay" format="reference"/>
<attr name="overlayHashContainer" format="reference"/>
<attr name="overlayHashContainerHash" format="reference"/>
<attr name="overlayRatingContainer" format="reference"/>
<attr name="overlayRatingContainerStar" format="reference"/>
<attr name="overlayRatingContainerRating" format="reference"/>
styles.xml

中的

我定义了类似

的类
<style name="NewsContainer.ItemWithImage">
    <item name="newsImage">@style/NewsContainerItemWithImageNewsImage</item>
    <item name="overlay">@style/NewsContainerItemWithImageOverlay</item>
    <item name="overlayHashContainer">@style/NewsContainerItemWithImageOverlayHashContainer</item>
    <item name="overlayHashContainerHash">@style/NewsContainerItemWithImageOverlayHashContainerHash</item>
    <item name="overlayRatingContainer">@style/NewsContainerItemWithImageOverlayRatingContainer</item>
    <item name="overlayRatingContainerStar">@style/NewsContainerItemWithImageOverlayRatingContainerStar</item>
    <item name="overlayRatingContainerRating">@style/NewsContainerItemWithImageOverlayRatingContainerRating</item>
</style>

<style name="NewsContainer.ItemWithOutImage">
    <item name="newsImage">@style/NewsContainerItemWithOutImageNewsImage</item>
    <item name="overlay">@style/NewsContainerItemWithOutImageOverlay</item>
    <item name="overlayHashContainer">@style/NewsContainerItemWithOutImageOverlayHashContainer</item>
    <item name="overlayHashContainerHash">@style/NewsContainerItemWithOutImageOverlayHashContainerHash</item>
    <item name="overlayRatingContainer">@style/NewsContainerItemWithOutImageOverlayRatingContainer</item>
    <item name="overlayRatingContainerStar">@style/NewsContainerItemWithOutImageOverlayRatingContainerStar</item>
    <item name="overlayRatingContainerRating">@style/NewsContainerItemWithOutImageOverlayRatingContainerRating</item>
</style>

通过致电

对我有用
context.setTheme(R.style.NewsContainer_ItemWithImage);
context.setTheme(R.style.NewsContainer_ItemWithOutImage);

在xml文件中,我添加了attrs.xml

的引用
<ImageView
        android:id="@+id/news_image"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        style="?newsImage" />