【发布时间】:2021-06-21 14:53:31
【问题描述】:
我尝试了两种方法来设置 EditText 的宽度,但没有得到正确的结果。 xml 文件包含:
<TextView
android:id="@+id/textView1"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/classname"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
style="@android:style/Widget.AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:hint="@string/teacher"
android:textSize="25sp" />
第一种方式:
TextView tw1 = (TextView) findViewById(R.id.textView1);
TextView tw2 = (TextView) findViewById(R.id.textView2);
tw2.setLayoutParams(new LinearLayout.LayoutParams(tw1.getLayoutParams().width, LinearLayout.LayoutParams.WRAP_CONTENT));
第二种方式:
TextView tw1 = (TextView) findViewById(R.id.textView1);
TextView tw2 = (TextView) findViewById(R.id.textView2);
tw2.setWidth(tw1.getWidth());
虽然第一个代码根本不起作用,但第二个代码给出了一个不相关的结果。我的错误是什么或这样做的正确方法是什么?
【问题讨论】:
标签: android android-layout layout textview