カスタムボタンのデフォルトの背景をnullに設定するにはどうすればよいですか。カスタムビューの既定のスタイル(属性)を提供する
意味... android:backgroundを "@null"に設定する "スタイル"を定義できます。 とレイアウトにスタイルを明示的に適用するようにユーザーに依頼してください。例:上記のコード
<style name="MyButton" parent="@android:style/Widget.Button">
<item name="android:background">@null</item>
</style>
と
<com.xxx.widget.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyButton"
android:text="MyButton" />
がうまく機能しています。 しかし、このスタイルをクラス "MyButton"に内部的に適用し、ユーザーにスタイルを明示的に設定させないようにするにはどうすればよいですか?以下のレイアウトを作成する方法例えば
は、以前のように動作します:
<com.xxx.widget.MyButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyButton" />
私は以下のようにコンストラクタでこれを実行しようとしましたが、その作業はありません。
public MyButton(Context context, AttributeSet attrs) {
this(context, attrs, com.xxx.R.style.MyButton);
}
PS。ユーザーが背景を明示的に設定していない場合、この「null」バックグラウンドを適用したいです。
私はあなたがあなたの質問のタイトルを変更すべきだと思います。あなたはいつもデフォルトスタイルを持つことができますが、それは後で変更することができます。おそらく、あなたは「最終的なスタイル」とかそのようなものを言います。 – ikbal
これについてのヒントについては、http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widgetを参照してください。 –
同様の要件がありました:http://stackoverflow.com/questions/31504413/theme-style-for-custom-view/31505624#31505624 – stefan