首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >android多屏幕权重分布

android多屏幕权重分布
EN

Stack Overflow用户
提问于 2015-12-21 08:19:06
回答 3查看 74关注 0票数 0

我正在创建这个布局..。使用形状作为背景,使布局看起来像一个具有多个标题的表。

代码语言:javascript
运行
复制
<?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>

问题是,当我在几个设备上测试这段代码时,无论是垂直还是水平,它们都不对齐,似乎每个设备的宽度或高度都会影响重量分布。

下面是每个文本视图中用作背景的可绘制单元格边框

代码语言:javascript
运行
复制
<?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英寸视图

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-21 11:24:22

试着在下面几行

代码语言:javascript
运行
复制
<?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>
票数 0
EN

Stack Overflow用户

发布于 2015-12-21 09:36:21

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),即空间的3/10,D将有4/10的空间。

如果您不想这样做,那么您需要使用100‘t这样的绝对值来表示宽度/高度。

霍普,这就说明了问题所在。酌情反映。

票数 0
EN

Stack Overflow用户

发布于 2015-12-21 10:35:37

@Mafdy Magdy,你在做一个小错误兄弟.,用我的代码代替.

代码语言:javascript
运行
复制
<?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

代码语言:javascript
运行
复制
<?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>

只要粘贴这个代码..。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34391417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档