当前位置: 首页 > 知识库问答 >
问题:

用Java改变我的Android应用程序的整个风格

何升
2023-03-14

我正在尝试根据已检查的单选按钮(参见下面的代码)以编程方式更改我的应用程序的整个样式(背景颜色、字体和字体大小)。我已经读到,要使其工作,我必须在onCreate方法中编写它,因为setTypeFace或setTextSize无法在其他函数中解析。

我还尝试在另一个函数中调用这两个方法,称为setStyles,但这两个函数仍然无法解析,我不知道为什么。我确实导入了android.graphics。字体的字体。

下面是我的Java代码和两个XML代码。

完整的Java代码:

public class MainActivity extends AppCompatActivity {

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

        LinearLayout buttonMenu = findViewById(R.id.dynamic_btn);

        Button showBtn = new Button(this);
        showBtn.setText(R.string.menu);
        showBtn.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        showBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                //setStyles(v);

                final View myView = getLayoutInflater().inflate(R.layout.style, null);
                final AlertDialog.Builder diag = new AlertDialog.Builder(getApplicationContext());
                diag.setTitle("Configuration de l'application");
                diag.setPositiveButton("Enregistrer", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        RadioGroup radioColGroup = (RadioGroup) ((Dialog)dialog).findViewById(R.id.radioColor);
                        int col = Color.parseColor("#000000"); // background color
                        int selectedId = radioColGroup.getCheckedRadioButtonId();

                        Typeface type = getResources().getFont(R.font.fff_tusj);

                        if (selectedId == R.id.style1) {
                            // change background, font and size
                            col = Color.parseColor("#000000");
                            type = getResources().getFont(R.font.fff_tusj);
                            v.setTextSize(40);
                        }

                        if (selectedId == R.id.style2) {
                            // change background, font and size
                            col = Color.parseColor("#00574A");
                            type = getResources().getFont(R.font.sansation_light);
                            v.setTextSize(50);
                        }

                        else if (selectedId == R.id.style3) {
                            // change background, font and size
                            col = Color.parseColor("#6B0504");
                            type = getResources().getFont(R.font.oswald_stencil);
                            v.setTextSize(30);
                        }

                        View background = findViewById(R.id.layoutBackground);
                        background.setBackgroundColor(col);
                        v.setTypeFace(type);
                    }
                });

                diag.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                });

                diag.setView(R.layout.style);
                diag.show();
            }
        });

        if(buttonMenu != null) {
            buttonMenu.addView(showBtn);
        }
    }

    public void setStyles(View view) {
        final View myView = getLayoutInflater().inflate(R.layout.style, null);
        final AlertDialog.Builder diag = new AlertDialog.Builder(this);
        diag.setTitle("Configuration de l'application");
        diag.setPositiveButton("Enregistrer", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                RadioGroup radioColGroup = (RadioGroup) ((Dialog)dialog).findViewById(R.id.radioColor);
                int col = Color.parseColor("#000000"); // background color
                int selectedId = radioColGroup.getCheckedRadioButtonId();

                Typeface type = Typeface.createFromAsset(getAssets(), "fff_tusj.ttf");

                if (selectedId == R.id.style1) {
                    // change background, font and size
                    col = Color.parseColor("#000000");
                    type = Typeface.createFromAsset(getAssets(), "oswald_stencil.ttf");
                    //myView.setTextSize();
                }

                if (selectedId == R.id.style2) {
                    // change background, font and size
                    col = Color.parseColor("#00574A");
                    type = Typeface.createFromAsset(getAssets(), "sansation_light.ttf");
                }

                else if (selectedId == R.id.style3) {
                    // change background, font and size
                    col = Color.parseColor("#6B0504");
                }

                View background = findViewById(R.id.layoutBackground);
                background.setBackgroundColor(col);
                //myView.setTypeFace(type);
            }
        });

        diag.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // do nothing
            }
        });

        diag.setView(R.layout.style);
        diag.show();
    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:id="@+id/layoutBackground">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/dynamic_btn"
            android:layout_weight="1"/>

        <EditText
            android:id="@+id/saisie_recherche"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:inputType="text"/>

        <Button
            android:id="@+id/recherche"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/recherche"/>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/resultats"/>

    <!-- dynamic menu -->

</LinearLayout>

style.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/style">

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioColor"
        android:orientation="horizontal"
        android:layout_margin="50px"
        android:gravity="center">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/style1"
            android:text="style 1"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/style2"
            android:text="style 2"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/style3"
            android:text="style 3"/>

    </RadioGroup>

</LinearLayout>

共有1个答案

曹铭晨
2023-03-14

如果有人对答案感兴趣,我实际上改变了我做事的方式。

为了更改字体,字体大小和背景颜色,我在布局文件样式中创建了新主题.xml并声明了三个新主题。

然后根据被点击的单选按钮(if条件),我使用了setTheme(nameOfTheme)。

它的工作原理。

 类似资料:
  • 我在我的项目中添加了一个外部库,由于它的值文件中有一个@string/app_name标记,所以它更改了我的整个应用程序名称。我不能编辑这个值文件。而且android studio更喜欢使用外部库app_name资源。我该怎么修好它? 解决方案: 正如Nicholas Tee所说:

  • 问题内容: 对于一个简单的Java桌面应用程序,我使用NetBeans IDE的助手添加了JFrame表单。对于此框架,我想更改标题栏中的图标。我尝试在生成的View类的构造函数的最后使用以下代码进行操作: String iconUrl绝对正确,据我从调试器透视图的变量概述判断,对象ii似乎还可以。但是,标题栏中的图标不会更改,它仍然是默认的Java图标。 为什么? 问题答案: 您可以尝试以下方法

  • 我想在我的应用程序中使用自定义字体。这是为整个应用程序提供字体的最佳方式。我知道如何将自定义字体分配给单个文本视图或按钮。 是否可以在一个地方提到自定义字体,例如在样式中。xml,字体将应用于整个应用程序(每个文本视图、按钮、编辑文本等)。

  • 本文向大家介绍Android编程之退出整个应用程序的方法,包括了Android编程之退出整个应用程序的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android编程之退出整个应用程序的方法。分享给大家供大家参考,具体如下: 我们在写android应用程序时,经常会遇到想退出当前Acitivity,或者直接退出应用程序.我之前的一般操作是按返回键,或者直接按home键直接返回,其实这

  • 问题内容: 我最近继承了一个小型Java程序,该程序从大型数据库中获取信息,进行一些处理并生成有关该信息的详细图像。原始作者使用单个线程编写了代码,然后对其进行了修改,以使其可以使用多个线程。 他在代码中定义了一个常量; 然后,它设置用于创建映像的线程数。 我理解他的理由,即线程数不能大于可用处理器的数目,因此将其设置为可以充分发挥处理器潜力的数量。这样对吗?还是有更好的方法来充分利用处理器的潜力

  • 我的问题基本上是Android Studio不会部署我的应用程序与我的新代码的变化。这是我的案例场景: 我有一个像这样工作的wifi direct代码(仅使用其方法进行测试): 上面的代码正常工作,然后我决定通过添加方法来更改它:setPeerDiscoveryHandler(booleanSissccess); 更改后,我的代码如下: 但是你猜怎么着,即使在完成了这个新代码并点击了Run按钮之后