我有LinearLayout
覆盖屏幕的整个宽度,我有一个随机数TextView
(本例中为7)。我希望TextView
的所有内容都相同,因此我将属性android:layout_weight="1"
放在每个中。
当TextView中没有文本时,一切正常:
但是当我添加与每个TextView
相对应的文字时,Textview
的尺寸会发生变化,而不是保持重量并调整LinearLayout
这只是我的Activity设计的一部分,所以我在一个新的xml中重新创建它以查看错误是否仍然存在。
而且我注意到只有当LinearLayout
位于HorizontalScrollView
如果不是,我会得到预期的结果:
我的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>
任何人都知道如何解决这个问题?提前谢谢。
答案 0 :(得分:1)
尝试这个我尝试使用表格布局
<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>
答案 1 :(得分:0)
我已经改变了你的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"
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>
答案 2 :(得分:0)
试试这段代码:
<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>