以编程方式设置 android:layout_centerHorizo​​ntal

发布于 2024-12-02 03:07:02 字数 184 浏览 0 评论 0原文

在 xml 中,您可以执行以下操作:

<TextView
    ...
    android:layout_centerHorizontal="true"
    ...
/>

当我拥有 TextView 实例时,我将如何以编程方式执行此操作?

In xml you can do the following:

<TextView
    ...
    android:layout_centerHorizontal="true"
    ...
/>

How would I, when I have the instance of TextView, do this programmatically?

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

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

发布评论

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

评论(4

冷心人i 2024-12-09 03:07:02

您应该使用 RelativeLayout.LayoutParams 类的 addRule 方法。

layoutparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mTextView.setLayoutParams(layoutParams);

You should use the addRule method of the RelativeLayout.LayoutParams class.

layoutparams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mTextView.setLayoutParams(layoutParams);
缺⑴份安定 2024-12-09 03:07:02

假设您有一个名为 tv: 的 TextView 存储在变量中,

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv.setLayoutParams(lp);

应该可以解决问题。

Assuming you have a TextView called stored in a variable tv:

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
tv.setLayoutParams(lp);

Should do the trick.

和影子一齐双人舞 2024-12-09 03:07:02

经过 10 分钟的战斗,我找到了如何在 Kotlin 中做到这一点:

注意 - 我正在使用视图绑定

val centerHorizontal = binding.tvOccupantName.layoutParams as RelativeLayout.LayoutParams
centerVertical.addRule(RelativeLayout.CENTER_HORIZONTAL)
binding.tvOccupantName.layoutParams = centerHorizontal

希望它有帮助!

After 10 minutes of fighting I found how to do it in Kotlin:

N.B. - I am using view binding

val centerHorizontal = binding.tvOccupantName.layoutParams as RelativeLayout.LayoutParams
centerVertical.addRule(RelativeLayout.CENTER_HORIZONTAL)
binding.tvOccupantName.layoutParams = centerHorizontal

Hope it helps!

谈情不如逗狗 2024-12-09 03:07:02

假设 txtPhone 是我们试图将其水平居中放置的文本视图。

如果您使用 Java,则使用以下代码,

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) txtPhone.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
txtPhone.setLayoutParams(layoutParams);

如果您使用 Kotlin,则使用以下代码,

val layoutParams = txtPhone.getLayoutParams() as RelativeLayout.LayoutParams
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
txtPhone.setLayoutParams(layoutParams)

Assume that txtPhone is the textview that we are trying to place it center in horizontal.

If you are using Java then use the following code,

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) txtPhone.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
txtPhone.setLayoutParams(layoutParams);

If you are using Kotlin then use the following code,

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