Android 如何在XML中定义屏幕大小的一半
我有以下以编程方式编写的代码Android 如何在XML中定义屏幕大小的一半,android,xml,xamarin,Android,Xml,Xamarin,我有以下以编程方式编写的代码 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" andro
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>
可以(宽度):
这意味着“宽度是未定义的,高度是,我说的重量是50%”(默认为1.0中的0.5),所以Android会将宽度扩展到设备屏幕宽度的50%
相反,将高度值切换到
0dp
,将宽度切换到某个值。我相信你现在明白了;) 试试这个,这是一个棘手的解决方案,但非常有效:
这是一个中间的伪视图,上面是GRIDVIEW!p>
<?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>
如果我是你,我的根布局将是垂直线性布局,我将使用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>
我很抱歉地说,这是一个丑陋的把戏,当API-1中有一些本机的东西来做这项工作时。android:layout\u height=“0dp”android:layout\u width=“match\u parent”android:layou weight=“0.5”
添加了该代码,但它不起作用。它应该这样做。请在你的问题下方张贴你的新布局(编辑)。我也需要GridLayout
周围的内容。好的,在另一个超级GridLayout
中有多个GridLayout
。然后看看答案:
<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>