我有一个垂直的 LinearLayout 和 TextView。
文件:unitdetails.xml
<LinearLayout
android:id="@+id/unit_details_person"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/unit_person_header"
android:orientation="vertical" />
文件:person_phone.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="0"
android:background="@android:color/darker_gray"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="15dp"
android:text="Some text">
</TextView>
我在我的 Activity 代码中添加了 TextView
while (iterator.hasNext()) {
Person person = iterator.next();
TextView positionTV = (TextView) getLayoutInflater().inflate(R.layout.person_position, null);
TextView personTV = (TextView) getLayoutInflater().inflate(R.layout.person_person, null);
TextView phoneTV = (TextView) getLayoutInflater().inflate(R.layout.person_phone, null);
TextView emailTV = (TextView) getLayoutInflater().inflate(R.layout.person_email, null);
positionTV.setText(person.getPosition());
personTV.setText(person.getPerson());
phoneTV.setText(person.getPhone());
emailTV.setText(person.getEmail());
Linkify.addLinks(phoneTV, Linkify.PHONE_NUMBERS);
Linkify.addLinks(emailTV, Linkify.EMAIL_ADDRESSES);
personLinearLayout.addView(positionTV);
personLinearLayout.addView(personTV);
personLinearLayout.addView(phoneTV);
personLinearLayout.addView(emailTV);
}
添加后,当我点击电话号码右侧时,TextView 会扩展整个屏幕宽度并启动拨号程序。
最佳答案
您是否尝试过像这样以编程方式设置参数:
myTextView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
并删除
android:layout_weight="0"
这里哪个没用?
关于android - 如何阻止 TextView 在 LinearLayout 中拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13049606/