如何将相对布局的宽度和高度安排为屏幕的一半?

5

我正在开发一款Android应用程序,我想将我的相对布局的宽度和高度设置为屏幕宽度和高度的一半。

谢谢


@varghesekutty,您需要将手机屏幕的高度和宽度减半。根据您的问题,我的答案是正确的,请不要减去集成。 - dipali
@dipali 谢谢你的回答。 - varghesekutty
4个回答

10
使用父布局作为 LinearLayout,并使用weightSumweight属性。

示例

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >

  <RelativeLayout
      android:id="@+id/button1"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="50" >

      <!-- Your RelativeLayout Items -->

  </RelativeLayout>

</LinearLayout>

2
*@SuppressLint("NewApi")
public static int getDeviceWidth(Activity activity) {
    int deviceWidth = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceWidth = size.x;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceWidth = display.getWidth();
    }
    return deviceWidth;
}

@SuppressLint("NewApi")
public static int getDeviceHeight(Activity activity) {
    int deviceHeight = 0;

    Point size = new Point();
    WindowManager windowManager = activity.getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        windowManager.getDefaultDisplay().getSize(size);
        deviceHeight = size.y;
    } else {
        Display display = windowManager.getDefaultDisplay();
        deviceHeight = display.getHeight();
    }
    return deviceHeight;
}*

以上两个函数是获取设备高度和宽度的。

使用这个函数,您可以获得屏幕尺寸的一半。
希望对您有用。


2
如何将相对布局分为两个相等的相对布局
    <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
</RelativeLayout>

将线性布局分为两个相等的相对布局

并将线性布局分为两个相等的相对布局

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1">
</RelativeLayout>
</LinearLayout>

我还没有尝试过第一种方法,但人们疯狂地提议使用LinearLayout作为解决方案。我会试一试。谢谢分享。 - Diolor
@Diolor,显然这在6年前欺骗了你。RelativeLayout中从未有过“weight”标签。 - Farid

1
你需要将RelativeLayout放在LinearLayout内,并使用android:weightsum=2android:layout_weight=1来获得所需的比例。
<?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"
    android:weightSum="2" >

    <RelativeLayout
        ....
        android:layout_weight=1
        ....>
    </RelativeLAyout>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接