【问题标题】:how to support styling of android library activity views?如何支持 android 库活动视图的样式?
【发布时间】:2015-11-19 03:48:25
【问题描述】:

我在一个库中提供一个活动,我想提供一个更改 UI 样式的规定,比如按钮背景颜色(带选择器)。如何做到这一点?我不想向用户公开我的 xml 文件。

【问题讨论】:

  • 提供 getter 和 setter 供您查看和那里的属性并向用户公开
  • Styleable 是最好的方法。

标签: android android-layout android-xml android-library


【解决方案1】:

存档的最佳方式在这里: http://developer.android.com/training/custom-views/create-view.html

例子:

public PieChart(Context context, AttributeSet attrs) {
   super(context, attrs);
   TypedArray a = context.getTheme().obtainStyledAttributes(
        attrs,
        R.styleable.PieChart,
        0, 0); 

   try { 
       mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
       mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
   } finally { 
       a.recycle();
   } 
} 

在您的资源中:(res/values/attrs.xml)

<resources>
   <declare-styleable name="PieChart">
       <attr name="showText" format="boolean" />
       <attr name="labelPosition" format="enum">
           <enum name="left" value="0"/>
           <enum name="right" value="1"/>
       </attr>
   </declare-styleable>
</resources>

最后你可以调用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:custom="http://schemas.android.com/apk/res/com.example.customviews">
 <com.example.customviews.charting.PieChart
     custom:showText="true"
     custom:labelPosition="left" />
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多