gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:44 26 4
gpt4 key购买 nike

我有以下自定义按钮 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com