2012-05-21 10 views
7

カスタムボタンのデフォルトの背景を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」バックグラウンドを適用したいです。

+0

私はあなたがあなたの質問のタイトルを変更すべきだと思います。あなたはいつもデフォルトスタイルを持つことができますが、それは後で変更することができます。おそらく、あなたは「最終的なスタイル」とかそのようなものを言います。 – ikbal

+1

これについてのヒントについては、http://stackoverflow.com/questions/4493947/how-to-define-theme-style-item-for-custom-widgetを参照してください。 –

+0

同様の要件がありました:http://stackoverflow.com/questions/31504413/theme-style-for-custom-view/31505624#31505624 – stefan

答えて

0

MyButton()コンストラクタで、なぜsetBackground(null)を呼び出さないのですか?

0

これはやや安価な方法ですが、背景を透明な四角に設定しないでください。このように - >

transparent_square.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/square" 
    android:> 

    <solid android:color="#00808080"></solid> // The first two zeros set the alpha 
               // to 0 

</shape> 

は、その後、あなたの描画可能にボタンの背景を設定する(描画可能ディレクトリに入れてください)。

ボタン:

<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send" 
     android:id="@+id/sendButton" 
     android:layout_weight="1" 
     android:background="@drawable/transparent_square"/> 
関連する問題