Android TextView 不填充宽度,为什么?

标签 android textview width fill-parent

我有以下布局并调用该布局。有一次,这段完全相同的代码会填满父布局的宽度,即一个选项卡。现在它没有,我不知道为什么。它阻止我发布我的最新版本。欢迎任何和所有输入。

section_title.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/seasonTitle"
    android:padding="3dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="#FFFFFFFF"
    android:textSize="14sp"
    android:textStyle="bold"
    android:maxHeight="26sp"
    android:background="#FFCC3333"
     />

这里是它的调用方式的 fragment :

TextView titleTv = (TextView)mInflater.inflate(R.layout.section_title, null);
titleTv.setText(titleText);
mMergeAdapter.addView(titleTv);

谢谢!

最佳答案

当你像这样在没有 parent 的情况下膨胀你的观点时:

 TextView titleTv = (TextView)mInflater.inflate(R.layout.section_title, null);

一些 android:layout_* 参数只是被丢弃了。为了避免这种情况,请使用指定的父级膨胀您的 View ,但不要将 View 附加到父级。像这样:

 TextView titleTv = (TextView)mInflater.inflate(R.layout.section_title, parent, false);

这应该可以解决您的问题。

关于Android TextView 不填充宽度,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7312481/

相关文章:

android - 类中的 TextView 和 setText

Android TextView 和 9 补丁布局不正确

html - 如何强制 DIV 中的元素占用尽可能多的水平空间?

CSS 高度 = 宽度?

android - 工具栏与布局中的其他小部件重叠

android - 如何将 ImageView 拖放到另一个 ImageView 上

Android 后台服务类 "onStartCommand"方法在设备启动时不执行其中的所有代码

Android EditText 不是多行

java - JSON 查询不更新 TextView

html - 无论内容长度如何,均等宽度的 flex 元素