如何在LinearLayout中阻止TextView拉伸

时间:2012-10-24 12:52:13

标签: android android-layout android-linearlayout textview

我有一个垂直的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>

我在我的活动代码中添加了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会扩展整个屏幕宽度和系统启动拨号程序。

1 个答案:

答案 0 :(得分:1)

您是否曾尝试以编程方式设置params:

myTextView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

并删除

android:layout_weight="0"

这在哪里没用?