android - 在java代码中动态地将样式应用于 View

标签 android styles custom-component

我有以下自定义按钮 View 。

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);
}

}

最佳答案

这是一个旧答案:几分钟前我得到了一个 -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
}

关于android - 在java代码中动态地将样式应用于 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11154310/

相关文章:

php - JQUERY ToolTip - 特定词的样式

android - 如何在 C# ComboBox 等微调器中获取选定值?

java - 正确使用 Facelet 模板和复合组件

java - 在网页中搜索..Android

android - ScrollView 和触摸监听器

android - 计时器不会在应用程序小部件 ListView 中更新

java - Android 预处理器

python - 如何在 Matplotlib 中更改文本颜色?

css - 仅适用于 Internet Explorer 的媒体查询

java - Android:Html TextView - 这可能吗?