android - 如何阻止 TextView 在 LinearLayout 中拉伸(stretch)

标签 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>

我在我的 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/

相关文章:

android - ConstraintLayout - 修复不同设备上不均匀的 dp

java - XML 转换为 JSON 时前导零被截断

android - 如何在不更改 View 索引的情况下将 View 置于 LinearLayout 的顶部?

Android Admob 没有足够的空间来展示广告

android - 在 android Oreo 中对传入的 SMS 运行服务

android - 为什么应用程序在第一次打开时会卡住一段时间。?

java - Android:编辑xml中定义的textview

android - 使用布局权重的方法是什么

android - 如何限制AlertDialog中动态Android EditText的行数

android - 使用LinearLayout将按钮放在屏幕底部?