分割屏幕分成四等份使用相对布局 [英] Divide Screen into Four Equal Parts using Relative Layout

查看:257
本文介绍了分割屏幕分成四等份使用相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有code,让我将屏幕分为四等份,并在各部分图像按钮。然而,code使用带来了建议,嵌套的权重是坏的表现线性布局。我怎样才能让使用相对布局我的布局?
下面是code,这里是预期的格式结果的图片

    
    

 <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_weight =1.0        机器人:方向=横向
        机器人:背景=#ff191919>        <切换按钮
            机器人:ID =@ + ID /按钮1
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:背景=#ff414141
            机器人:layout_weight =1.0
            机器人:文字=飞机
            机器人:layout_margin =5DP
            机器人:TEXTSIZE =20SP
            安卓的onClick =airplaneClicked/>
        <的ImageButton
            机器人:ID =@ + ID /按钮2
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:背景=#ff414141
            机器人:layout_weight =1.0
            机器人:layout_margin =5DP
            机器人:scaleType =fitCenter
            机器人:SRC =@绘制/ ic_brightness/>
    < / LinearLayout中>    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:layout_weight =1.0
        机器人:方向=横向
        机器人:背景=#ff191919> 01        <的ImageButton
            机器人:ID =@ + ID /按钮3
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:背景=#ff414141
            机器人:layout_weight =1.0
            机器人:layout_margin =5DP
            机器人:scaleType =fitCenter
            机器人:SRC =@绘制/ ic_sound/>
        <的ImageButton
            机器人:ID =@ + ID / settingsbutton
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:背景=#ff414141
            机器人:layout_weight =1.0
            机器人:layout_margin =5DP
    机器人:scaleType =fitCenter
    机器人:SRC =@绘制/设置2/>    < / LinearLayout中>
    < / LinearLayout中>


解决方案

试试这个code你的RelativeLayout的。

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / R1
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent>    < RelativeLayout的
        机器人:ID =@ + ID / R3
        机器人:layout_width =match_parent
        机器人:layout_height =225dp
        机器人:layout_marginBottom =10dp
        机器人:layout_alignParentTop =真正的>        <的ImageButton
            机器人:ID =@ + ID / I1
            机器人:layout_width =150dp
            机器人:layout_height =match_parent
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:背景=@绘制/ ic_launcher/>        <的ImageButton
            机器人:ID =@ + ID / I2
            机器人:layout_width =150dp
            机器人:layout_height =match_parent
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentTop =真
            机器人:背景=@绘制/ ic_launcher/>
    < / RelativeLayout的>    < RelativeLayout的
        机器人:ID =@ + ID / R2
        机器人:layout_width =match_parent
        机器人:layout_height =225dp
        机器人:layout_alignParentBottom =真
        机器人:layout_below =@ + ID / R3>        <的ImageButton
            机器人:ID =@ + ID / I3
            机器人:layout_width =150dp
            机器人:layout_height =match_parent
            机器人:layout_alignParentLeft =真
            机器人:layout_alignParentTop =真
            机器人:背景=@绘制/ ic_launcher/>        <的ImageButton
            机器人:ID =@ + ID / 0-14
            机器人:layout_width =150dp
            机器人:layout_height =match_parent
            机器人:layout_alignParentRight =真
            机器人:layout_alignParentTop =真
            机器人:背景=@绘制/ ic_launcher/>
    < / RelativeLayout的>< / RelativeLayout的>

I currently have code that allows me to divide the screen into four equal parts, with an image button in each part. However, the code uses linear layouts which brings up the suggestion "nested weights are bad for performance". How can I make my layout using relative layouts? Below is the code, and here is a picture of the intended format

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"

        android:orientation="horizontal"
        android:background="#ff191919">

        <ToggleButton
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:text="Plane"
            android:layout_margin="5dp"
            android:textSize="20sp"
            android:onClick="airplaneClicked"/>


        <ImageButton
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_brightness"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1.0"
        android:orientation="horizontal"
        android:background="#ff191919">01

        <ImageButton
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_sound" />


        <ImageButton
            android:id="@+id/settingsbutton"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff414141"
            android:layout_weight="1.0"
            android:layout_margin="5dp"
    android:scaleType="fitCenter"
    android:src="@drawable/settings2"/>

    </LinearLayout>
    </LinearLayout>

解决方案

Try this code for your RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout
        android:id="@+id/r3"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:layout_marginBottom="10dp"
        android:layout_alignParentTop="true" >

        <ImageButton
            android:id="@+id/i1"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/i2"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/r2"
        android:layout_width="match_parent"
        android:layout_height="225dp"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/r3" >

        <ImageButton
            android:id="@+id/i3"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/i4"
            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>

</RelativeLayout>

这篇关于分割屏幕分成四等份使用相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆