6

次のカスタム ボタン ビューがあります。

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>

拡張ボタンはこのスタイルになりません。私が間違っていることを指摘できますか?解決策を検索したところ、これが見つかりまし。誰かが何かを提案できますか

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

}
4

5 に答える 5

3

これは古い答えです:私は数分前に-1を取得しました、そしてここにあります

新しいソリューション

基本的な考え方は、コンストラクターに属性を渡すことです。完全な解決策については、このリンクを確認してください:Javaコードで動的にビューにスタイルを適用する

古いソリューション(機能していません)

次のコンストラクターをクラスに追加します。

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
}
于 2012-06-25T18:51:50.880 に答える
2

なぜ使用しないのですかsetTextAppearance(Context context, int resid);。テキストの色、サイズ、スタイル、ヒントの色、ハイライトの色を設定できます。

PrayerTimeLabelクラスでは、

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

詳細については、この投稿を参照してください:カスタム属性を参照するコードによる setTextAppearance

于 2012-06-26T18:43:28.927 に答える
1

このトピックの古い(そしてより単純な)回答は次のとおりです

要約:

ContextThemeWrapper newContext = new ContextThemeWrapper(baseContext, R.style.MyStyle); ボタン = 新しいボタン (newContext);

于 2017-02-13T17:46:54.657 に答える
0

リンク先のバグレポートには次のように記載されています。

さらに、実行時に要素を動的に作成する場合、コードでスタイルを単純に適用することはできません。構築しているビュー用に別の XML レイアウトを作成し、それを拡張する必要があります [...]

これを念頭に置いて、ソリューションは次のようになります。

ボタンの xml レイアウトを作成します。このレイアウトを、prayertimebutton.xml とします。

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

次に、クラスのinitializePrayerTimeButtonメソッドで、ボタンを拡張してレイアウトPrayerControlに追加します。PrayerTimeLabelPrayerControl

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

の 2 番目のパラメーターはLayoutInflater.inflate()、親 View(Group) への参照であることに注意してください。

Google の Android フレームワーク エンジニアである adamp による以下の回答では、このアプローチについて詳しく説明しています。

Android でコードからスタイルを設定するのがなぜ複雑なのか

于 2012-06-28T18:06:46.743 に答える
0

コンストラクターで、コンストラクターへの呼び出しを行う必要があります。super(context,attrs,defStyle)これにより、 が に適用さdefStyleれますView

ただし、動的に変更することはできませんstyle

于 2012-06-22T10:25:05.243 に答える