以编程方式重新设置 TextView 高度

发布于 2025-01-03 23:22:10 字数 687 浏览 2 评论 0原文

我想在将 textView 添加到 xml 文件中的主窗口后重置它的高度。

在RelativeLayout中,

  <TextView
      android:id="@+id/text_l"
      android:layout_width="50sp"
      android:layout_height="50sp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginLeft="10sp"
      android:layout_marginTop="145dp"
      android:gravity="center"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textColor="#000000" >
  </TextView>

我只想将其从50更改为70:

我尝试过:

 TextView text = (TextView)findViewById(R.id.text_l);
 text.setHeight(70);

但没有任何改变。

I want to reset a textView height after I have added it to the main window in the xml file.

inside a RelativeLayout,

  <TextView
      android:id="@+id/text_l"
      android:layout_width="50sp"
      android:layout_height="50sp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginLeft="10sp"
      android:layout_marginTop="145dp"
      android:gravity="center"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:textColor="#000000" >
  </TextView>

I just want to change it from 50 to 70:

I tried:

 TextView text = (TextView)findViewById(R.id.text_l);
 text.setHeight(70);

but nothing changed.

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

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

发布评论

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

评论(6

多彩岁月 2025-01-10 23:22:10

您应该通过 LayoutParams 更改它:

LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);

编辑

您不应在代码中使用以像素为单位的大小,而应使用尺寸:

dimens.xml:

<dimen name="text_view_height">50dp</dimen>

在代码中:

params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);

You should change it via LayoutParams:

LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 70;
textView.setLayoutParams(params);

EDIT

You should not use sizes in pixels in you code, use dimensions for this:

dimens.xml:

<dimen name="text_view_height">50dp</dimen>

In code:

params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
十年九夏 2025-01-10 23:22:10

实际上,您可以设置文本视图高度,例如:

private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;

Pragmatically you can set textview height like:

private TextView mTxtView;
int height = 50; //your textview height
mTxtView.getLayoutParams().height = height;
茶色山野 2025-01-10 23:22:10

您可以通过以下方式动态设置textview的宽度和高度

private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay(); 
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();


LayoutParams params = mTxtView.getLayoutParams();
    params.width = screenWidth-30;
    mTxtView.setLayoutParams(params);

you can dynamically set width and height of textview by

private TextView mTxtView;
private int screenWidth, screenHeight;
Display display = getWindowManager().getDefaultDisplay(); 
    screenWidth = display.getWidth();
    screenHeight = display.getHeight();


LayoutParams params = mTxtView.getLayoutParams();
    params.width = screenWidth-30;
    mTxtView.setLayoutParams(params);
感情洁癖 2025-01-10 23:22:10

在 Kotlin 中,使用 DP 到像素转换

  changeTextHeight.setOnClickListener { view ->

         // random height for testing
        val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10

        // set Height in pixels
        hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
         //refresh layout
        hello.requestLayout()


    }

将 DP 转换为像素,请参阅这篇文章

fun convertDpToPixel(dp: Float, context: Context): Int {
        return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
    }

In Kotlin with DP to pixels translation

  changeTextHeight.setOnClickListener { view ->

         // random height for testing
        val randomHeightInDP = Random().nextFloat() * (50.0f - 10.0f) + 10

        // set Height in pixels
        hello.layoutParams.height = convertDpToPixel(randomHeightInDP, applicationContext)
         //refresh layout
        hello.requestLayout()


    }

Convert DP to pixels, see this post:

fun convertDpToPixel(dp: Float, context: Context): Int {
        return (dp * (context.resources.displayMetrics.densityDpi.toFloat() / DisplayMetrics.DENSITY_DEFAULT)).toInt()
    }
攀登最高峰 2025-01-10 23:22:10

如果您想

首先使用 dimen,则应在 dimen XML 文件中设置大小。

<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>

或者

text_l.getLayoutParams().height = 
     getResources().getDimensionPixelSize(R.dimen.text_height);

text_l.getLayoutParams().width = 
     getResources().getDimensionPixelSize(R.dimen.text_width);

如果

您只想设置 int (例如我们想设置 50dp 高度和 100dp 宽度)

text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;

the sample way for this if you want to use dimen

first, you should set size in dimen XML file.

<dimen name="text_height">50dp</dimen>
<dimen name="text_width">50dp</dimen>

and

text_l.getLayoutParams().height = 
     getResources().getDimensionPixelSize(R.dimen.text_height);

text_l.getLayoutParams().width = 
     getResources().getDimensionPixelSize(R.dimen.text_width);

Or

if you want to set just int (for example we wanna set 50dp height and 100dp width)

text_l.getLayoutParams().height = 50 * 3;
text_l.getLayoutParams().width= 100 * 3;
花心好男孩 2025-01-10 23:22:10

我知道这是一个老问题,但为了其他人可能会发现这个问题,在某些情况下您应该在更改布局参数后调用 textView.requestLayout() 。如果您只是在绘制布局之前一次性更改布局参数,则可以忽略它。就我而言,我想使用 onCheckedChangedListener 基于单选按钮选择更改 TextView 的高度参数,但 TextView 高度只会在第一次绘制时更新。添加 requestLayout() 解决了这个问题。

TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass 
    tv.requestLayout();//request a layout pass
 }

I know this is an old question, but for the sake of others who might find this, there are scenarios where you should call textView.requestLayout() after changing the layout parameters. While it my be fine to omit it if you are simply changing the layout parameters as a one time thing before the layout is drawn. In my case, I wanted to change the height parameter of a TextView based on a radio button selection using onCheckedChangedListener, but the TextView height would only update the first time it was drawn. Adding requestLayout() solved this problem.

TextView tv;
ViewGroup.LayoutParams params = tv.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
if(!tv.isInLayout()) {//make sure it isn't currently in a layout pass 
    tv.requestLayout();//request a layout pass
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文