[英]How to prevent a TextView resize the parent LinearLayout - Android
I have a LinearLayout
that covers the entire width of the screen, inside I have a random number of TextView
(7 in this example). 我有一个
LinearLayout
可以覆盖屏幕的整个宽度,里面有一个随机数的TextView
(在此示例中为7)。 I want all the TextView
are the same size, so I put the attribute android:layout_weight="1"
in each. 我希望所有
TextView
的大小均相同,因此我将android:layout_weight="1"
放入其中。
When there is not text in the TextView everything works fine: 当TextView中没有文本时,一切正常:
but when I add the text corresponding to each TextView
, the size of Textview
change, not keep the weight and resize the LinearLayout
但是当我添加与每个
TextView
对应的文本时, Textview
的大小Textview
更改,而不是保持权重并调整LinearLayout
大小
This is only part of the design of my Activity, so I recreated it in a new xml to see if the error persists. 这只是我的Activity设计的一部分,因此我在新的xml中重新创建了它,以查看错误是否仍然存在。 And I have noticed that this only happens when the
LinearLayout
is inside of a HorizontalScrollView
而且我注意到只有在
LinearLayout
位于HorizontalScrollView
内时才会发生这种情况
When it is not, I get the expected result: 如果不是,我会得到预期的结果:
My xml: 我的xml:
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:singleLine="true"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
</HorizontalScrollView>
Anyone know how to fix this? 有人知道怎么修这个东西吗? thanks in advance.
提前致谢。
try this i tried with table layout 试试这个我试过表布局
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<TableLayout
android:id="@+id/table_dashboared"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow
android:id="@+id/tablerow_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="7" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textSize="12sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
I have changed your xml a bit. 我对您的xml进行了一些更改。 Try this and let me know the result
试试这个,让我知道结果
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="7">
<TextView
android:singleLine="true"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:singleLine="true"
android:layout_marginLeft="1dp"
android:text="LONG TEXT"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
Try this code: 试试这个代码:
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="7" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXTTTTTTTTTTTTTTTTTTTTTTTTTT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#009FDC"
android:gravity="center"
android:padding="5dp"
android:singleLine="true"
android:text="LONG TEXT"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
</HorizontalScrollView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.