从代码动态更改多个文本视图的大小(没有“磁盘上”xml 主题)?

发布于 2024-10-08 03:39:01 字数 224 浏览 3 评论 0原文

我的代码中有 10 个 TextView,我想更改所有这些的字体大小。在我的布局上,我使用了 @style 来定义通用属性,但是我不知道一旦布局出现在屏幕上如何从代码中更改它们。

我不想做的是更新 N 个对象,但只在一个地方写入。我知道我可以使用applyTheme,但这假设您在磁盘上的某处有一个XML主题,我希望能够将字体缩放到任何大小,因此这个解决方案不切实际。

有什么想法吗?

I have 10 TextViews in my code and i would like to change font size on all of them. On my layout i have used a @style to define common properties however i don't know how to change them all from code once layout is on screen.

What i dont want to do is update N objects but write only in one place. I know that i could use applyTheme but this assumes that you have an XML theme somewhere on disk, i want to be able to scale fonts to any size so this solution is not practical.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-10-15 03:39:01

查看类似的问题:

如何以编程方式在视图中设置样式属性< /a>

android 在运行时动态改变样式

android:在运行时设置textView样式

听起来不可能在运行时更改单个样式元素。您可以应用不同的主题,但随后您需要为您想要支持的每种字体大小使用不同的主题。很烦人,但有可能。

否则,您必须想出一种方法来减少更新所有文本视图的痛苦。您可以将大小存储在应用程序类中,以便所有活动都可以访问它,然后在 onCreate 中更新每个 TextView 的大小。

See similar questions:

How to programmatically setting style attribute in a view

android dynamically change style at runtime

android : set textView style at runtime

It sounds like its not possible to change an individual style element at runtime. You could apply a different theme, but then you would need a different theme for every font size you want to support. Annoying, but possible.

Otherwise, you have to come up with a way to reduce the pain of updating all of the textViews. You could store the size in your application class, so it is accessible by all of your activities, and then in onCreate update the size for each TextView.

时间海 2024-10-15 03:39:01

的完整代码

以下是动态更改字体大小和主题MainActivity

public class MainActivity extends AppCompatActivity {

    TextView textViewMain;
    Button buttonChangeFont;
    int size, value;
    SharedPreferences pref;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);

        changeTheme();

        setContentView(R.layout.activity_main);

        //References
        textViewMain = findViewById(R.id.textViewMain);
        buttonChangeFont = findViewById(R.id.buttonChangeFont);

        //Set Onclick on button
        buttonChangeFont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent goToPreview = new Intent(MainActivity.this, PreviewActivity.class);
                startActivity(goToPreview);
            }
        });

        int value = pref.getInt("value", 0);
        textViewMain.setTextSize(value);
    }

    private void changeTheme() {

        String theme = pref.getString("theme", "default");
        if (theme != null && theme.equals("default")) {
            setTheme(R.style.AppTheme);
        } else if (theme != null && theme.equals("black")) {
            setTheme(R.style.BlackTheme);
        } else {
            setTheme(R.style.BlueTheme);
        }
    }

    public void getSize() {


        editor = pref.edit();
        size = getIntent().getIntExtra("size", 14);

        if (size == 14) {
            value = 14;
            editor.putInt("value", value);

        } else if (size == 20) {
            value = 20;
            textViewMain.setTextSize(value);
            editor.putInt("value", value);

        } else {
            value = 30;
            textViewMain.setTextSize(value);
            editor.putInt("value", value);
        }
        editor.commit();
    }

    @Override
    protected void onStart() {
        getSize();
        super.onStart();
    }
}

PreviewActivity

public class PreviewActivity extends AppCompatActivity {
    TextView textViewSmall, textViewMedium, textViewLarge, textViewPreview;
    Button buttonOK;
    String size;
    int value;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        setContentView(R.layout.activity_preview);

        //TextView References
        textViewLarge = findViewById(R.id.textViewLarge);
        textViewMedium = findViewById(R.id.textViewMedium);
        textViewSmall = findViewById(R.id.textViewSmall);
        textViewPreview = findViewById(R.id.textViewPreview);

        //Button References
        buttonOK = findViewById(R.id.buttonOK);

        //Set Onclick listener to TextView and Button
        textViewSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(14);
                size = "small";
            }
        });

        textViewMedium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(20);
                size = "medium";
            }
        });

        textViewLarge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(30);
                size = "large";
            }
        });


        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (size) {
                    case "small":
                        value = 14;
                        break;

                    case "medium":
                        value = 20;
                        break;

                    case "large":
                        value = 30;
                        break;

                }

                Intent i = new Intent(PreviewActivity.this, MainActivity.class);
                i.putExtra("size", value);
                startActivity(i);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_catalog.xml file.
        // This adds menu items to the app bar.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        editor = pref.edit();

        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {

            case R.id.default_theme:

                textViewPreview.setBackgroundColor(Color.WHITE);
                textViewPreview.setTextColor(Color.DKGRAY);

                editor.putString("theme", "default");
                editor.commit();
                changeTheme();
                return true;

            case R.id.black_theme:

                textViewPreview.setBackgroundColor(Color.BLACK);
                textViewPreview.setTextColor(Color.WHITE);

                editor.putString("theme", "black");
                editor.commit();
                changeTheme();
                return true;

            case R.id.blue_theme:

                textViewPreview.setBackgroundColor(Color.BLUE);
                textViewPreview.setTextColor(Color.RED);

                editor.putString("theme", "blue");
                editor.commit();
                changeTheme();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void changeTheme() {

        String theme = pref.getString("theme", "default");
        if (theme != null && theme.equals("default")) {
            setTheme(R.style.AppTheme);
        } else if (theme != null && theme.equals("black")) {
            setTheme(R.style.BlackTheme);
        } else {
            setTheme(R.style.BlueTheme);
        }
    }
}

我正在使用自己的自定义主题。要创建自定义主题,您可以参考这篇关于自定义主题创建的文章

Here is the whole code for dynamically change font size and theme

MainActivity:

public class MainActivity extends AppCompatActivity {

    TextView textViewMain;
    Button buttonChangeFont;
    int size, value;
    SharedPreferences pref;
    SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pref = getApplicationContext().getSharedPreferences("MyPref", 0);

        changeTheme();

        setContentView(R.layout.activity_main);

        //References
        textViewMain = findViewById(R.id.textViewMain);
        buttonChangeFont = findViewById(R.id.buttonChangeFont);

        //Set Onclick on button
        buttonChangeFont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent goToPreview = new Intent(MainActivity.this, PreviewActivity.class);
                startActivity(goToPreview);
            }
        });

        int value = pref.getInt("value", 0);
        textViewMain.setTextSize(value);
    }

    private void changeTheme() {

        String theme = pref.getString("theme", "default");
        if (theme != null && theme.equals("default")) {
            setTheme(R.style.AppTheme);
        } else if (theme != null && theme.equals("black")) {
            setTheme(R.style.BlackTheme);
        } else {
            setTheme(R.style.BlueTheme);
        }
    }

    public void getSize() {


        editor = pref.edit();
        size = getIntent().getIntExtra("size", 14);

        if (size == 14) {
            value = 14;
            editor.putInt("value", value);

        } else if (size == 20) {
            value = 20;
            textViewMain.setTextSize(value);
            editor.putInt("value", value);

        } else {
            value = 30;
            textViewMain.setTextSize(value);
            editor.putInt("value", value);
        }
        editor.commit();
    }

    @Override
    protected void onStart() {
        getSize();
        super.onStart();
    }
}

PreviewActivity:

public class PreviewActivity extends AppCompatActivity {
    TextView textViewSmall, textViewMedium, textViewLarge, textViewPreview;
    Button buttonOK;
    String size;
    int value;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pref = getApplicationContext().getSharedPreferences("MyPref", 0);
        setContentView(R.layout.activity_preview);

        //TextView References
        textViewLarge = findViewById(R.id.textViewLarge);
        textViewMedium = findViewById(R.id.textViewMedium);
        textViewSmall = findViewById(R.id.textViewSmall);
        textViewPreview = findViewById(R.id.textViewPreview);

        //Button References
        buttonOK = findViewById(R.id.buttonOK);

        //Set Onclick listener to TextView and Button
        textViewSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(14);
                size = "small";
            }
        });

        textViewMedium.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(20);
                size = "medium";
            }
        });

        textViewLarge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textViewPreview.setTextSize(30);
                size = "large";
            }
        });


        buttonOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (size) {
                    case "small":
                        value = 14;
                        break;

                    case "medium":
                        value = 20;
                        break;

                    case "large":
                        value = 30;
                        break;

                }

                Intent i = new Intent(PreviewActivity.this, MainActivity.class);
                i.putExtra("size", value);
                startActivity(i);
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_catalog.xml file.
        // This adds menu items to the app bar.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        editor = pref.edit();

        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {

            case R.id.default_theme:

                textViewPreview.setBackgroundColor(Color.WHITE);
                textViewPreview.setTextColor(Color.DKGRAY);

                editor.putString("theme", "default");
                editor.commit();
                changeTheme();
                return true;

            case R.id.black_theme:

                textViewPreview.setBackgroundColor(Color.BLACK);
                textViewPreview.setTextColor(Color.WHITE);

                editor.putString("theme", "black");
                editor.commit();
                changeTheme();
                return true;

            case R.id.blue_theme:

                textViewPreview.setBackgroundColor(Color.BLUE);
                textViewPreview.setTextColor(Color.RED);

                editor.putString("theme", "blue");
                editor.commit();
                changeTheme();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void changeTheme() {

        String theme = pref.getString("theme", "default");
        if (theme != null && theme.equals("default")) {
            setTheme(R.style.AppTheme);
        } else if (theme != null && theme.equals("black")) {
            setTheme(R.style.BlackTheme);
        } else {
            setTheme(R.style.BlueTheme);
        }
    }
}

I am using my own custom theme. To create a custom theme you can reference this article on custom theme creation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文