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

Set width in xml but height in code

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

在 xml 试试这个:

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

还有这个在你的 Class:

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

尺寸xml:

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