多屏幕android权重分配
我正在创建这个布局。。。使用形状作为背景使我的布局看起来像一个有多个标题的表格多屏幕android权重分配,android,android-layout,Android,Android Layout,我正在创建这个布局。。。使用形状作为背景使我的布局看起来像一个有多个标题的表格 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:orientati
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:adjustViewBounds="true"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_weight="3"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_weight="3"
android:background="@drawable/cellborder" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:adjustViewBounds="true"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:layout_weight="1"
android:background="@drawable/cellborder" />
</LinearLayout>
</LinearLayout>
问题是,当我在垂直和水平环境中跨多个设备测试此代码时,它们彼此没有正确对齐,似乎每个设备的宽度或高度都会影响重量分布
这是每个文本视图中用作背景的可绘制单元格边框
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res /android" android:shape="rectangle" >
<stroke android:width="0.1dp" android:color="@color/black"
/>
<padding android:left="0dp" android:top="0dp"
android:right="0dp" android:bottom="0dp" />
</shape>
我真的不知道如何解决这个问题,有什么建议吗?
平板电脑7英寸视图Android布局:权重属性用于在水平或垂直视图之间分配可用空间。
假设有四个视图的权重为A:1、B:2、C:3、D:4。
然后,A将得到1/(1+2+3+4),即空间的十分之一。 B将得到2/(1+2+3+4),即空间的2/10。 同样,C将获得3/(1+2+3+4),即空间的十分之三,D将获得空间的十分之四。 如果您不希望这样,那么您需要使用绝对值,如100dp表示宽度/高度。
希望,这就解释了这一点。
适当地反思。@Mafdy Magdy,你犯了一点小错误,兄弟,用我的代码替换吧
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:adjustViewBounds="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="3"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.2"
android:adjustViewBounds="true"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="@drawable/cellborder" />
</LinearLayout>
</LinearLayout>
在cellborder.xml中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="0.1dp"
android:color="@android:color/black" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
只需粘贴此代码….请在下面的行中尝试
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
百分比支持库FTW@Mafdy Magdy这是强制使用形状的吗???我有另一个解决方案,在布局中使用权重。@Rumit的想法是使用列或边框进行表格设计。。。。我使用形状就是为了这样做,我相信也可以用分隔符来做,但这里的问题是重量和对齐那些列/bordershi@@Mafdy Magdy。。。。有什么解决方案吗???为什么要在每个视图上设置android:adjustViewBounds?不要这样做,这可能是因为你这样做的形状(正方形)干扰重力。不工作。。。用绝对值设置宽度并不能使它起作用,您也必须使用它。重量或绝对值。如果对其中一个视图使用绝对值,则剩余空间将被分配给其他视图。希望这有帮助。我不确定绝对值是否能使它在每个android设备上工作。我测试了这个,它工作得很好,我应该用textview替换线性布局还是将textview放在布局中?这会有什么不同吗?谢谢,请随时向我寻求帮助……)我想知道是否有可能在这个设计下面为listview设计一个行样式。。。同样的设计理念