开发者社区> 问答> 正文

使用Java更改Android应用程序的整体样式

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

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

以下是我的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>

展开
收起
几许相思几点泪 2019-12-15 20:14:43 602 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载