我试图用TableLayout中的四个TextView来实现40%-20%-20%-20%的分割(或与之相近的东西)。layout_weight在TableLayout中没有兑现
问题是,最左边一列需要太多空间。换句话说,右边的3列不占用他们需要的空间。 如果需要,左侧较长的文本应该分成多行,如,而不是右侧的三个较短的文本。
红色标注的文字应该不两行进行分割。
我即使在最左边的列试android:maxLines="1"
在右侧列,android:lines="2"
,并仍intented将无法正常工作(→有只有一条线路好的,但最右边的列会被切去)。
我也试过this suggestion并在最左边的列上设置了android:layout_width="0dp"
。 那么,这给了我一个相反的问题:该列太少空间(长标题被裁剪),而右侧3太多。与android:layout_width="0dp"
我不想硬编码任何DP宽度
相反的问题重视为列,以更好地支持不同的屏幕。对我来说,指定layout_weight
的相对宽度恰恰是我所需要的,只要我能够正常工作......任何想法我做错了什么?
Android 4.4.2。我的测试设备是Nexus 7,在这种情况下,我在纵向模式下遇到了这个问题。
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
布局XML一行(对应于第一屏幕截图以上):
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<TextView
android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="center_vertical|left"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center"
android:text="(todo)"
android:textSize="16dp"
android:layout_marginTop="8dp"
/>
<TextView
android:id="@+id/pages_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center"
android:textSize="16dp"
android:maxLines="1"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_vertical|right"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
/>
</TableRow>
周边TableLayout
和ScrollView
:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- table rows added dynamically in code -->
</TableLayout>
</ScrollView>
我建议使用一个网格布局(是网格布局不GridView的),而不是TableLayout。 – GareginSargsyan
其实,我发现一个简单的解决方案,而坚持使用TableLayout:http://stackoverflow.com/a/20751799/56285 – Jonik
学习使用GridLayout将使它值得你的wile。它比TableLayout更灵活。 – GareginSargsyan