使用ContextThemeWrapper在安卓中更改样式

3
我看到了好几个与此类似的问题,但是我无法成功地将它应用到我的情况中。实际上,我正在尝试使用ContextThemeWrapper在用户单击时更改按钮的样式。因此,最初该按钮设置为XML文档中链接的某种样式,并且我包含此代码以尝试通过使用ContextThemeWrapper来改变其外观,根据以下建议在第一个链接中。问题(我想)是我不知道如何将新创建的按钮链接到我在屏幕上看到的物理对象。欢迎任何建议!谢谢!
    public class MainActivity extends AppCompatActivity {

    private Button TestButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TestButton = findViewById(R.id.button);

        TestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContextThemeWrapper newContext = new ContextThemeWrapper(getApplicationContext(), R.style.custom);

                 TestButton = new TextView(newContext);

            }
        });
    }
}

这是我指的链接: 使用ContexThemeWrapper更改样式 有关不同上下文的一般问题

如果您能更清楚地表达问题,并提供您所提到的其他线程的链接,那么这个问题将会吸引到更好的答案。 - MikaelF
2个回答

0

您有几个选项可以动态更改按钮的样式。

1. 手动更改每个属性:
TestButton.setTextColor(Color.RED);
TestButton.setTextSize(24);
//等等
2. 创建一个新按钮并替换布局中的旧按钮:
ContextThemeWrapper newContext = new ContextThemeWrapper(MainActivity.this, R.style.custom);
NewTestButton = new NewTestButton(newContext);
Container.removeView(TestButton)
Container.addView(NewTestButton)

你好Bracadabra,感谢您抽出时间!实际上现在它有所作为(小胜利),虽然离期望的结果还很远。我尝试将定义按钮的所有内容都链接到样式资源中(包括布局限制),但它们不起作用。因此,当我将新按钮带入场景时,它的位置偏离了应该在的位置,而且似乎唯一遵循的属性是textColor,这似乎很奇怪... - H1991
你好,没有代码很难确定问题出在哪里。 - Bracadabra

0

同意,没有代码很难... 这是我目前完成的内容:

public class MainActivity extends AppCompatActivity {
    private Button TestButton;
    private Button NewTestButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TestButton = (Button) findViewById(R.id.button);

    }

    public void doSomething(View view) {
        ViewGroup parent = (ViewGroup) view.getParent();

        parent.removeView(view);
        ContextThemeWrapper newContext = new ContextThemeWrapper(MainActivity.this, R.style.custom);
        NewTestButton = new Button(newContext);
        parent.removeView(view);
        parent.addView(NewTestButton);

    }
}

这是关于主活动的XML文件

<Button
        android:id="@+id/button"
        style="@style/original"
        android:onClick="doSomething"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

And both styles applied before and after the click...

<style name="custom">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginTop">200dp</item>
        <item name="android:background">@android:color/black</item>
        <item name="android:text">Custom Button</item>
        <item name="android:textColor">@android:color/white</item>

    </style>
    <style name="original">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginTop">200dp</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:text">Original Button</item>
        <item name="android:textColor">@android:color/black</item>

    </style>

问题在于样式中无法包含有关约束的数据,因此一旦我删除视图并创建新视图,该视图位置就不正确。此外,背景/文本颜色的效果也无法实现... 我只能通过LayoutInflater获得成功的结果... 尽管我很想掌握这种方法... 感谢您的帮助!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接