在Java代码中动态地将样式应用于视图

时间:2012-06-22 10:18:03

标签: android styles custom-component

我有以下自定义按钮视图。

public class PrayerTimeLabel extends Button {

int hours;
int minutes;
String dayHalf; //am or pm
Context parentActivity;
PrayerControl parentControl;

public PrayerTimeLabel(Context context,PrayerControl parent) {
    super(context);                 
    init(context,parent,0);
}

public PrayerTimeLabel(Context context, int defStyle, PrayerControl parent) {
    //super(context, null, R.style.Button_PrayerTimeButton);
    super(context, null, defStyle);             
    init(context,parent,defStyle);
}


private void init(final Context context, PrayerControl parent, int defStyle)
{        
    parentActivity = context;
    parentControl = parent;
    Typeface tf = Typeface.createFromAsset(context.getAssets(),"fonts/digital.ttf");
    this.setTypeface(tf);       
    this.setText(false);

    this.setOnClickListener(new OnClickListener() {         
        public void onClick(View v) {
            TimeDialog dialogBox = parentControl.getDialogBox();
            dialogBox.setTime(hours, minutes, dayHalf);
            dialogBox.show();
        }
    });
}


public void setTime(int hrs, int min, String half,boolean signalParent)
{
    hours = hrs;
    minutes = min;
    dayHalf = half;
    this.setText(signalParent);
}

public void setText(boolean signalParent)
{
    super.setText(String.format("%02d", hours)+":"+String.format("%02d", minutes)+" "+dayHalf);
    if(signalParent){
        parentControl.setPrayerTime(hours, minutes, dayHalf);
    }
}

}

我在style.xml中定义了以下样式

    <style name="Button.PrayerTimeButton" parent="@android:style/TextAppearance.Widget.Button">
    <item name="android:background">#000</item>
    <item name="android:textSize">18dp</item>
    <item name="android:textColor">#FFFF00</item>
</style>

扩展按钮没有获得此样式。有人可以指出我做错了吗?我搜索了解决方案并找到了this。有人可以提出一些建议吗

注意:我无法使用XML来应用样式。它必须是构造函数。

编辑:

以下是创建和使用此自定义按钮的类。我删除了许多不相关的代码行

public class PrayerControl extends LinearLayout {


protected PrayerTimeLabel prayerTimeButton;

protected String prayerName;
protected static int counter=0;


public PrayerControl(Context context) {
    super(context);
    init(context);
}

public PrayerControl(Context context, AttributeSet attrs) {
    super(context, attrs);
    getXMLAttributes(context, attrs);
    init(context);
}

    protected void getXMLAttributes(Context context, AttributeSet attrs)
{
    TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.PrayerControl);

    prayerName = a.getString(R.styleable.PrayerControl_name);
    dayHalf = a.getString(R.styleable.PrayerControl_dayHalf);
    hours = a.getInteger(R.styleable.PrayerControl_hours, 4);
    minutes = a.getInteger(R.styleable.PrayerControl_minutes, 30);

    ltrProgress = a.getInteger(R.styleable.PrayerControl_postNamazInterval, 0);
    rtlProgress = a.getInteger(R.styleable.PrayerControl_preNamazInterval, 0);
    intervalMax = a.getInteger(R.styleable.PrayerControl_intervalMax, 30);


    a.recycle();

}

protected void init(Context context)
{
    counter++;
    parentActivity = context;
    this.setOrientation(LinearLayout.HORIZONTAL);
    this.setId(counter);    

    prayerTimeButtonStyle = R.style.Button_PrayerTimeButton;
    initializePrayerTimeButton();

}


protected void initializePrayerTimeButton()
{
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            40);        
    params.gravity = Gravity.CENTER;
    //params.weight  = 1.0f;

    prayerTimeButton = new PrayerTimeLabel(parentActivity,prayerTimeButtonStyle,this);
    prayerTimeButton.setTime(hours, minutes, dayHalf,false);

    prayerTimeButton.setLayoutParams(params);
    this.addView(prayerTimeButton);
}

}

5 个答案:

答案 0 :(得分:3)

这是一个古老的答案:几分钟前我得到-1,这是

新解决方案

基本思想是将属性传递给构造函数。 请查看此链接以获取完整的解决方案:Applying style to views dynamically in java code

旧解决方案(不工作)

将这些构造函数添加到您的类中:

public StyledButton(Context context) {
    super(context, null, R.style.Button_PrayerTimeButton);
//... whatever
}

public StyledButton(Context context, AttributeSet attrs) {
    super(context, attrs, R.style.Button_PrayerTimeButton);
//... whatever
}

public StyledButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, R.style.Button_PrayerTimeButton);
//... whatever
}

答案 1 :(得分:3)

为什么不使用setTextAppearance(Context context, int resid);。它允许您设置文本颜色,大小,样式,提示颜色和突出显示颜色。

PrayerTimeLabel班,

private void init(final Context context, PrayerControl parent, int defStyle)
{        
    setTextAppearance(context, R.style.Button_PrayerTimeButton);
    ...
}

有关详细信息,请参阅此帖子:setTextAppearance through code referencing custom attribute

答案 2 :(得分:1)

以下是本主题的旧答案(更简单): https://stackoverflow.com/a/21455192/4360308

汇总:

ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext,R.style.MyStyle); button = new Button(newContext);

答案 3 :(得分:0)

您应该在构造函数中调用super(context,attrs,defStyle)构造函数,该构造函数将defStyle应用于View

但是你不能改变style dinamically。

答案 4 :(得分:0)

您链接到州的错误报告:

  

此外,在运行时动态创建元素时,这意味着您不能简单地在代码中应用样式。您需要为正在构建的视图创建单独的XML布局,并对其进行充气[...]

考虑到这一点,解决方案可能如下所示:

为您的按钮创建一个xml布局 - 让我们称之为prayertimebutton.xml:

<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFF00"
        //...etc
/>

然后,在initializePrayerTimeButton课程的PrayerControl方法中,充气并将PrayerTimeLabel按钮添加到PrayerControl布局:

//Retrieve a LayoutInflater instance that is hooked up to the current context
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//The LayoutInflater instantiates the layout file into a PrayerTimeLabel object
PrayerTimeLabel button = (PrayerTimeLabel)inflater.inflate(R.layout.prayertimebutton, this, false);
//add the PrayerTimeLabel to the PrayerControl
this.addView(button);

请注意,LayoutInflater.inflate()的第二个参数是对父视图(组)的引用。

以下来自Google的Android框架工程师adamp的回答更详细地讨论了这种方法。

Why so complex to set style from code in Android