【问题标题】:Create "theme" for custom Android view containing multiple inner text views为包含多个内部文本视图的自定义 Android 视图创建“主题”
【发布时间】:2015-05-05 19:47:42
【问题描述】:

我有一个自定义视图“address_view.xml”,它显示一个人的姓名和街道地址,定义如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="vertical">

    <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="John Smith"/>

    <TextView
            android:id="@+id/street_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="2"
            tools:text="1 Main St."/>
</LinearLayout>

此视图在我的应用程序的多个页面中使用,但每个 TextView 有 2 种字体、文本颜色和大小的变化。

是否可以为这个视图创建“主题”,分别设置名称和地址 TextViews 的 textFont/textColor?例如,我想做这样的事情:

<com.example.view.AddressView
    ...
    style="@style/Theme1" />

将“名称”TextView 设置为使用 FontA、ColorA 和 Size1,并将“地址”TextView 设置为使用 FontB、ColorB 和 Size2。

这样,我可以在某些页面上使用 Theme1,并使用字体/颜色/大小的第二种组合创建另一个“Theme2”并在其他页面上使用它。

【问题讨论】:

    标签: android android-view android-styles


    【解决方案1】:

    您首先需要定义自定义属性,然后在样式中使用它们。作为示例,我将使用三角形样式。

    首先定义你想用你的属性改变什么并将它们放入/res/attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="Triangle">
            <attr name="triangleColor" format="color"/>
            <attr name="triangleStrokeColor" format="color"/>
            <attr name="triangleStrokeWidth" format="dimension"/>
        </declare-styleable>
    </resources>
    

    并且在您的自定义视图中,您需要读取您传入的值

     // Get the values from XML
    TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.Triangle, style, 0);
    
    int tmp;
    
    mTriangleColor = typedArray.getColor(R.styleable.Triangle_triangleColor, mTriangleColor);
    mStrokeColor = typedArray.getColor(R.styleable.Triangle_triangleStrokeColor, mStrokeColor);
    
    tmp = typedArray.getDimensionPixelSize(R.styleable.Triangle_triangleStrokeWidth, -1);
    mStrokeWidth = tmp != -1 ? tmp : 2 * density; // Use 2dp as a default value
    
    // Don't forget this!
    typedArray.recycle();
    

    然后定义样式。 注意: 自定义属性项不需要 xml 命名空间,因此没有 android:

    <style name="defaultTriangle">
        <item name="triangleColor">#FF33B5E5</item>
        <item name="triangleStrokeColor">@android:color/black</item>
        <item name="triangleStrokeWidth">3dp</item>
    </style>
    

    然后申请

    <some.package.Triangle
        style="@style/defaultTriangle"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:padding="10dp"
        android:rotation="0"
        />
    

    【讨论】:

    • 这对文本字体有什么作用?我的字体作为单独的 .ttf 文件包含在 assets/ 文件夹中。
    • 您创建一个字符串类型的属性,然后将字体的位置作为字符串提供。在您看来,您阅读了该路径并使用它
    猜你喜欢
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    相关资源
    最近更新 更多