3

テーマと特定のボタン デザインを宣言できます。

<style name="MyTheme" parent="android:style/Theme.Black">
  <item name="android:buttonStyle">@style/Button.b1</item>
</style>

<style name="Button.b1" parent="android:style/Widget.Button">
    <item name="android:textColor">#000000</item>
</style>

問題は、このスタイルがすべてのボタンに適用されることです。他のボタン (「保存」ボタンのようなもの) とは独立して各テーマを変更する独自のスタイルで特定のボタンを宣言したいと思います。何か案が?


私は次のことを試しました:

<style name="Button.MyButton" parent="android:style/Widget.Button">
  <item name="android:background">@drawable/shape</item>
</style>

 <style name ="Button.MyButton.Theme1">
     <item name="android:textColor">#000000</item>
  </style>

 <style name ="Button.MyButton.Theme2">
     <item name="android:textColor">#FFFFFF</item>
  </style>

 <Button
     android:id="@+id/save_button" 
     android:layout_width="0px" 
     style="@style/Button.MyButton"
     android:layout_weight="1"
     android:layout_height="wrap_content"
     android:text="@string/save"/>

これでボタンはテーマから独立しましたが、上で宣言したスタイル属性も適用する必要があります。現時点では、MyButton スコープで宣言されている属性のみが適用され、MyButton.Theme1 または MyButton.Theme2 では適用されません。

4

3 に答える 3

3

MyThemeボタンのスタイルを定義するためだけにテーマが使用されている場合は、それを削除してください。parentまた、ボタンからプロパティを削除します。

<style name="Button.b1">
    <item name="android:textColor">#000000</item>
</style>

次に、レイアウトで、以下のように、必要なボタンにのみスタイルを使用します。

 <Button
         android:id="@+id/btn_custom"
         style="@style/Button.b1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" >
 </Button>
于 2012-04-24T10:05:24.360 に答える
1

MyTheme宣言を削除して、選択したボタンのプロパティstyles.xmlに簡単に設定できます:)@style/Button.b1android:style

編集:あなたが求めているものをついに手に入れたと思います。私は自分で試したことはありませんが、うまくいくはずです:

<!-- Declare your themes here -->
<style name="Theme1" parent="android:style/Theme.Black"></style>
<style name="Theme2" parent="android:style/Theme.Black"></style>

<!-- Declare your "abstract" Button style -->
<style name="MyButton" parent="android:style/Widget.Button">
  <item name="android:background">@drawable/shape</item>
</style>

<!-- Override MyButton style for Theme1 -->
<style name ="Theme1.MyButton" parent="@style/MyButton">
  <item name="android:textColor">#000000</item>
</style>

<!-- Override MyButton style for Theme2 -->
<style name ="Theme2.MyButton" parent="@style/MyButton">
  <item name="android:textColor">#FFFFFF</item>
</style>
于 2012-04-24T09:52:27.413 に答える
0

Button.b1そのようにスタイルを設定したいボタンに対してのみ、スタイルを直接使用する必要があります。これらのボタンのandroid:styleプロパティを使用します。

android:style="Button.b1"
于 2012-04-24T09:55:54.820 に答える