简体   繁体   English

在xml中设置宽度,在代码中设置高度

[英]Set width in xml but height in code

如何在xml中设置视图(例如textview)的宽度,但是以编程方式定义其高度,我在android开发人员文档中没有看到任何内容,并且想知道是否有人可以帮助我?

Try this in xml: 在xml中尝试:

 <TextView
            android:id="@+id/tv"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="HelloWorld"/>

And this one in your Class: 这是您班上的一位:

TextView textView = (TextView)findViewById(R.id.tv);
LayoutParams params = textView.getLayoutParams();
params.height = getResources().getDimensionPixelSize(R.dimen.text_view_height);
textView.setLayoutParams(params);

dimens xml: 使XML变暗:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM