如何仅为一个LinearLayout应用通用样式

时间:2017-02-20 23:24:44

标签: android android-layout

我已经阅读过关于在AndroidManifest中应用整个应用程序的样式,但我还没有找到如何在Linear Layout中应用样式。

例如,我想更改:所有这些TextView的大小,填充和文本对齐:

 <LinearLayout
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="30"
        style="@style/ItemTransacationHeader">
        <!--android:theme="@style/ItemTransacationHeader">-->


        <TextView
            android:text="Client"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView8"
            android:layout_weight="10"
            />

        <TextView
            android:text="Quantity"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView9"
            android:layout_weight="10"
            />

        <TextView
            android:text="Product Value"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/textView12"
            android:layout_weight="10"
            />


    </LinearLayout>

所以我创建了风格

<style name="ItemTransacationHeader" parent="android:Theme">
    <item name="android:textSize">10dp</item>
    <item name="android:textAlignment">textStart</item>
    <item name="android:padding">4dp</item>
</style>

我已尝试以各种方式将该样式应用于LinearLayout

style="@style/ItemTransacationHeader"
android:theme="@style/ItemTransacationHeader"
app:theme="@style/ItemTransacationHeader"

但我还没有想到这是否可能(我想是的)我怎么能实现呢?

3 个答案:

答案 0 :(得分:1)

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="0dp"
    android:layout_width="match_parent"
    android:layout_weight="30">

    <TextView
        android:text="Client"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView8"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

    <TextView
        android:text="Quantity"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView9"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

    <TextView
        android:text="Product Value"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/textView12"
        android:layout_weight="10"
        style="@style/ItemTransacationHeader"/>

</LinearLayout>

答案 1 :(得分:0)

您需要为每个@style单独设置TextView

答案 2 :(得分:0)

为TextView创建样式(您已完成此步骤,为了清晰起见重命名)

<style name="AppTextStyle" parent="android:Theme">
    <item name="android:textSize">10dp</item>
    <item name="android:textAlignment">textStart</item>
    <item name="android:padding">4dp</item>
</style>

在应用程序的主题中将其定义为TextView的样式:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    ...
    <item name="android:textViewStyle">@style/AppTextStyle</item> 
</style>

具有该主题的活动中的所有TextView现在都应使用AppTextStyle

查看现有问题:Setting global styles for Views in Android