线性布局中的自动调整文本大小与重量 android java

发布于 2025-01-14 02:17:21 字数 378 浏览 4 评论 0原文

我想自动调整文本大小。但它是在 LinearLayout 中,我正在使用权重。我尝试过类似的事情:

                            app:autoSizeTextType="uniform"
                            android:autoSizeMinTextSize="2sp"
                            android:autoSizeMaxTextSize="100sp"
                            android:autoSizeStepGranularity="2sp"

但没有任何效果!有人可以帮助我吗??? (我搜索了很多但没有任何效果)谢谢!

I would like to auto-size my text. But it is in linearLayout and I m using weight. I tried things like that :

                            app:autoSizeTextType="uniform"
                            android:autoSizeMinTextSize="2sp"
                            android:autoSizeMaxTextSize="100sp"
                            android:autoSizeStepGranularity="2sp"

but nothing worked ! Can someone help me ??? (I searched a lot but nothing worked) Thank you !

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

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

发布评论

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

评论(2

日暮斜阳 2025-01-21 02:17:21

autosize 的工作原理是为视图提供恒定的宽度和宽度。 height ant 然后它会适当地自动调整文本大小。避免在宽度上使用 wrap_content...如果可能的话,尝试给出精确的宽度/高度,或者您使用 wrap_content 具有可变高度,但使用数字或 match_parent 固定宽度

autosize works on the principal that you give the view constant width & height ant then it will autosize the text appropriately. Refrain from using wrap_content on width... if possible try to give exact width/height or you have have variable height with wrap_content but fixed width with number or match_parent

給妳壹絲溫柔 2025-01-21 02:17:21

好吧,我解决了我的问题:

我正在做的是获取relativelayout的大小,而我的textview在里面。然后,我以编程方式调整 TextView 的尺寸,并使用 setAutoTypeUniform 函数使文本适合我的布局。

rl_get_coins.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rl_get_coins.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            height = rl_get_coins.getHeight(); //height is ready
            width = rl_get_coins.getWidth(); //height is ready

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text_get_coins.getLayoutParams();
            params.height = height;
            params.width = width;
            text_get_coins.setLayoutParams(params);
            text_get_coins.setText("Obtenir");
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(text_get_coins, 1, 17, 1,
                    TypedValue.COMPLEX_UNIT_DIP);
        }
    });

好吧,伙计们,如果你们有任何问题,请告诉我。我很高兴它对我有用!

由 stackoverflow 制作,很高兴,我做到了哈哈

Well, I resolved my problem :

What I'm doing is taking the size of a RelativeLayout, and my TextView is inside. Then I dimensioned the TextView programmatically and use setAutoTypeUniform function to fit the text in my layout.

rl_get_coins.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rl_get_coins.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            height = rl_get_coins.getHeight(); //height is ready
            width = rl_get_coins.getWidth(); //height is ready

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text_get_coins.getLayoutParams();
            params.height = height;
            params.width = width;
            text_get_coins.setLayoutParams(params);
            text_get_coins.setText("Obtenir");
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(text_get_coins, 1, 17, 1,
                    TypedValue.COMPLEX_UNIT_DIP);
        }
    });

well guys, if you have any problem, tell it to me. I so happy that it works for me !

Made by a made by stackoverflow, so happy, I did it lol

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