我需要使用一组线性布局构建布局。布局必须占据屏幕的定义百分比。我需要这样做才能在所有设备中看到类似的外观。
我在右上角的布局中有一个textview(绿色框)。每当我在textview中添加一些数据时,它会扰乱整个布局,如下图所示。但是当我到达屏幕/布局的右端时,我需要自动包装数据。
请帮我解决这个问题。在此先感谢
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="91"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="40"
android:weightSum="235">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ff0000"
android:layout_weight="100">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff00"
android:layout_weight="45">
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tvItemName" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:layout_weight="90">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="60"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="30"
android:background="#00ab00"
android:weightSum="100">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:text="Test data" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#cd00ab"
android:layout_weight="70">
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ab0000"
android:layout_weight="9">
</LinearLayout>
</LinearLayout>
答案 0 :(得分:9)
将您的layout_width
或layout_height
作为0dp而不是wrap_content
。 (如果它是垂直布局,layout_height
应该是0dp;如果它是水平布局,layout_width
应该是0dp)
请注意layout_weight
描述了在分配了layout_width
s / layout_height
之后如何划分剩余空间。< / p>
wrap_content
并不意味着“进行文本换行”;它表示将该维度设置为视图的“首选”维度。
答案 1 :(得分:5)
使用此XML文件。我已经为你做了改变。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="91"
android:orientation="horizontal"
android:weightSum="100" >
<!-- Below is the first modification to layout_width -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:orientation="vertical"
android:weightSum="235" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100"
android:background="#ff0000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="45"
android:background="#ffff00"
android:orientation="vertical" >
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tvItemName" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ViewFlipper>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#ffffff"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<!-- Below is the second modification to layout_width -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="30"
android:background="#00ab00"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="Test data Test dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest data"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="70"
android:background="#cd00ab"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
android:background="#ab0000" >
</LinearLayout>
</LinearLayout>
对我来说很好看。看看下面的快照。