【问题标题】:How to define half of the screen size in XML如何在 XML 中定义屏幕大小的一半
【发布时间】:2015-06-18 15:00:02
【问题描述】:

我有以下以编程方式编写的代码,

Display display = this.WindowManager.DefaultDisplay;
Point size = new Point();
int height = size.Y;

bViewLayout.Height = (int)(height * 0.5);

但是,我想知道如何在 Xml 中实现屏幕的一半而不是硬编码如下?

<GridLayout
    android:layout_columnSpan="1"
    android:layout_gravity="fill"
    android:layout_rowSpan="1"
    android:layout_height="135"/>

已编辑: 以下代码不起作用:

   <?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnCount="2"
    android:rowCount="2">
    <GridLayout
        android:id="@+id/aView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="2"
        android:layout_width="100"
        android:layout_height="25"
        android:text="1" />
    <GridLayout
        android:id="@+id/bView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:text="2" />
    <GridLayout
        android:id="@+id/cView"
        android:layout_columnSpan="1"
        android:layout_gravity="fill"
        android:layout_rowSpan="1"
        android:layout_height="0dp"
        android:layout_width="match_parent"
        android:layout_weight="0.5"
        android:text="3" />
</GridLayout>

【问题讨论】:

    标签: android xml xamarin


    【解决方案1】:

    这样就可以了(对于宽度):

    <GridLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" />
    

    这意味着“宽度未定义,高度未定义,我说的重量是 50%”(默认情况下为 1.0 中的 0.5),因此 Android 会将该宽度扩展到设备屏幕宽度的 50%。

    相反,将高度值切换为0dp,将宽度值切换为某个值。我确定你现在明白了;)

    【讨论】:

    • android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="0.5",添加了该代码,但它不起作用。
    • 应该的。请在您的问题下方发布您的新布局(编辑它)。
    • 我也需要GridLayout 周围的东西。
    • 好的,所以你在另一个超级 GridLayout 中有多个 GridLayouts。然后看到那个答案:stackoverflow.com/a/16393481/603270
    【解决方案2】:

    试试这个,这是一个棘手的解决方案,但非常有效:

    它包含在一个 relativeLayout 中,中间有一个假视图和上面的 gridview!

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black">
        <View 
            android:id="@+id/fakeView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_centerVertical="true"/>
        <GridLayout
            android:layout_columnSpan="1"
            android:layout_gravity="fill"
            android:layout_rowSpan="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/fakeView"
            android:background="@color/white"/>
    </RelativeLayout>
    

    【讨论】:

    • 我很抱歉地说,但是当 API-1 有本地的东西来完成这项工作时,这是一个丑陋的把戏。
    【解决方案3】:

    如果我是你,我的根布局将是垂直 LinearLayout,我会使用 weightSum 属性。

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:gravity="center"
    android:orientation="vertical" >
    
       <Your_first_half_layout_type
        android:layout_width="match_parent"
        android:layout_height="0dp"
        layout_weight="1"
        />
    
        <Your_second_half_layout_type
        android:layout_width="match_parent"
        android:layout_height="0dp"
        layout_weight="1" />
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      相关资源
      最近更新 更多