如何使用主题(不可绘制)在代码中为按钮设置样式?
How can I do I set style for Button in code using theme (not drawable)?
我有一个静态按钮,我不得不将其更改为代码,但是当我更改它并通过 R.style.ThemeOverlay_MyDarkButton
在代码中指定相同的主题时
我发现没有应用任何样式。
所以我改了下面的
<LinearLayout
android:id="@+id/buttons_pane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3.0">
<Button
android:id="@+id/button_start_verification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@string/start_phone_auth"
android:theme="@style/ThemeOverlay.MyDarkButton"/>
</LinearLayout>
和
mStartButton = (Button) findViewById(R.id.button_start_verification);
至
<LinearLayout
android:id="@+id/buttons_pane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3.0">
</LinearLayout>
和
LinearLayout buttons_pane = findViewById(R.id.buttons_pane);
mStartButton = new Button(this, null, R.style.ThemeOverlay_MyDarkButton);
mStartButton.setText(R.string.start_phone_auth);
mStartButton.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f));
mStartButton.setId(R.id.button_start_verification);
buttons_pane.addView(mStartButton);
PS:我知道使用可绘制对象 xml 作为背景图像来设置样式的方法
(Android Button Styling Programmatically) 但我的问题是如何使用主题
您的按钮样式和主题是两种不同的东西。您以编程方式创建的按钮更像是设置
style="@style/ThemeOverlay.MyDarkButton"
而不是
android:theme="@style/ThemeOverlay.MyDarkButton"
你想要的。
您可以使用 ContextThemeWrapper 以编程方式应用主题。
Context themedContext = new ContextThemeWrapper(this,
R.style.ThemeOverlay_MyDarkButton);
mStartButton = new Button(themedContext);
我有一个静态按钮,我不得不将其更改为代码,但是当我更改它并通过 R.style.ThemeOverlay_MyDarkButton
在代码中指定相同的主题时
我发现没有应用任何样式。
所以我改了下面的
<LinearLayout
android:id="@+id/buttons_pane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3.0">
<Button
android:id="@+id/button_start_verification"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@string/start_phone_auth"
android:theme="@style/ThemeOverlay.MyDarkButton"/>
</LinearLayout>
和
mStartButton = (Button) findViewById(R.id.button_start_verification);
至
<LinearLayout
android:id="@+id/buttons_pane"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3.0">
</LinearLayout>
和
LinearLayout buttons_pane = findViewById(R.id.buttons_pane);
mStartButton = new Button(this, null, R.style.ThemeOverlay_MyDarkButton);
mStartButton.setText(R.string.start_phone_auth);
mStartButton.setLayoutParams(new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f));
mStartButton.setId(R.id.button_start_verification);
buttons_pane.addView(mStartButton);
PS:我知道使用可绘制对象 xml 作为背景图像来设置样式的方法 (Android Button Styling Programmatically) 但我的问题是如何使用主题
您的按钮样式和主题是两种不同的东西。您以编程方式创建的按钮更像是设置
style="@style/ThemeOverlay.MyDarkButton"
而不是
android:theme="@style/ThemeOverlay.MyDarkButton"
你想要的。
您可以使用 ContextThemeWrapper 以编程方式应用主题。
Context themedContext = new ContextThemeWrapper(this,
R.style.ThemeOverlay_MyDarkButton);
mStartButton = new Button(themedContext);