【问题标题】:Best View to achive this layout (To give dimension in ratios or percentage in screen)实现此布局的最佳视图(在屏幕中以比率或百分比给出尺寸)
【发布时间】:2016-02-19 08:23:19
【问题描述】:

您建议使用什么视图来在 Android 中完成此布局:

---------------------------------------
|           Cell 1          |         |
-----------------------------  Cell 4 |
|   Cell 2    |  Cell 3     |         |  
---------------------------------------

单元格的内容是标题和小文本。单元格 2 和单元格 3 的大小应与单元格 1 相同,宽度应与单元格 1 相同。单元格 1 应约为屏幕大小的 3/4,单元格 4 应为屏幕大小的 1/4。

【问题讨论】:

  • 单元格 1 和单元格 4 的黑白比例是多少? @Thommy
  • 好吧,你可以通过使用 1.) GridView 与 col 和 row span 2.) RelativeLayout 3.) 使用 layout_weight 或百分比库的嵌套线性布局
  • 我同意@RobinVinzenz,您可以使用 3). 嵌套线性布局轻松完成此布局

标签: android android-layout android-widget


【解决方案1】:
<LinearLayout
android:orientation="horizontal">
     <LinearLayout
           android:orientation="vertical>
           Create cell1
           <LinearLayout
                  android:orientation="horizontal">
            Create cell 2
            Create cell 3
           </LinearLayout>

     </LinearLayout>
     Create cell 4
</LinearLayout>

使用布局权重来获得适当的间距

【讨论】:

  • 通过这种方式我遇到了问题,文本被剪切了。
  • 哪个文本被剪掉了?我的意思是哪个单元格?
  • 您可以为例如指定权重 - 对于第一个线性布局,权重为 3 和 1
  • 单元格 2 和 3 中的文本。
  • 您的单元格 2 和单元格 3 的组合大小应该是屏幕的 3/4 吧?和 cell2 /cell 3 将是 3/8。如果您使用上述建议的方法,它将创建您所需的布局。文本被剪切是另一个问题。
【解决方案2】:

我建议使用GridLayout:

<GridLayout 
android:orientation="horizontal"
android:columnCount="2"
android:rowCount="3" >

<YourCell1View
    android:layout_rowSpan="2"/>

<YourCell2View
    android:layout_column="1" />

<YourCell3View  
    android:layout_column="1"
    android:layout_row="1" />

<YourCell4View
    android:layout_row="2"
    android:layout_columnSpan="2" />

</GridLayout>

当然,这只是一个基本布局,您必须将其余的单元格视图布局添加到此。但这应该会给你你的结果。

【讨论】:

    【解决方案3】:
    <?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="200dp" <!--choose height according to you need-->
        android:orientation="horizontal"
        android:weightSum="10">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="7"
            android:orientation="vertical"
            android:weightSum="10">
    
            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:background="@android:color/holo_blue_light" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:orientation="horizontal"
                android:weightSum="10">
    
                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="5"
                    android:background="@android:color/holo_green_dark" />
    
                <View
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="5"
                    android:background="@android:color/holo_green_light" />
            </LinearLayout>
    
    
        </LinearLayout>
    
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:background="@android:color/holo_red_light" />
    
    </LinearLayout>
    

    带有权重的嵌套线性布局可以帮助您在所有屏幕上实现相同的比例

    替代方案:-

    嵌套权重通常被认为不利于性能。 您可以在支持库中找到可用于此目的的 PercentLayout。这个有两个直接导数

    • 百分比相对布局
    • PercentFrameLayout

    我在这里使用了 PercentRelativeLayout 你可以通过添加来获得它

    compile 'com.android.support:percent:23.0.0'
    

    给你毕业

     <android.support.percent.PercentRelativeLayout                        
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="200dp">
    
            <View
                android:id="@+id/vwCell4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:background="@android:color/holo_red_dark"
                app:layout_widthPercent="30%" />
    
            <View
                android:id="@+id/vwCell1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@id/vwCell4"
                android:background="@android:color/holo_orange_dark"
                app:layout_heightPercent="50%"
                app:layout_widthPercent="70%" />
    
            <View
                android:id="@+id/vwCell2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@id/vwCell1"
                android:background="@android:color/holo_green_light"
                app:layout_heightPercent="50%"
                app:layout_widthPercent="35%" />
    
            <View
                android:id="@+id/vwCell3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/vwCell1"
                android:layout_toLeftOf="@id/vwCell4"
                android:layout_toRightOf="@id/vwCell2"
                android:background="@android:color/holo_blue_dark"
                app:layout_heightPercent="50%"
                app:layout_widthPercent="35%" />
    
    
        </android.support.percent.PercentRelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      相关资源
      最近更新 更多