简体   繁体   English

使用相对百分比布局创建间距

[英]Create Spacing with Percentage Relative Layout

My problem is that I have four Buttons on the screen, I used a PercentageRelativeLayout to place them where I want them and everything was working great. 我的问题是,屏幕上有四个Button,我使用了PercentageRelativeLayout将其放置在所需的位置,并且一切正常。
They even resize! 他们甚至调整大小!
However, when they do resize, the Buttons start touching each other, and when I make it so they do not touch each other on a tablet, when I go back to a phone the space is too far apart. 但是,当它们调整大小时,这些按钮开始彼此接触,而当我这样做时,它们在平板电脑上不会彼此接触,当我回到手机时,空间距离太远了。

Does anyone have any idea on how I could create a spacing that could work on all devices? 有人对我如何创建可以在所有设备上使用的间距有任何想法吗?

Because I thought that PercentageRelativeLayouts were the perfect solution to get things to resize, without using any LinearLayout and it is not working out the way I thought. 因为我认为PercentageRelativeLayouts是无需调整任何线性布局即可调整大小的理想解决方案,因此无法解决我的想法。

Below is my code 下面是我的代码

<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="match_parent"
android:background="#696969">

<ImageButton
    app:layout_widthPercent="40%"
    app:layout_heightPercent="40%"
    app:layout_marginLeftPercent="9%"
    app:layout_marginTopPercent="21%"
    android:background="#00ff0000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/opt1"
    android:scaleType="fitCenter"
    android:id="@+id/purpleButton"
    android:layout_align="@+id/option1"
    android:longClickable="false" />

<ImageButton
    app:layout_widthPercent="40%"
    app:layout_heightPercent="40%"
    app:layout_marginLeftPercent="9%"
    app:layout_marginTopPercent="58%"
    android:background="#00ff0000"
    android:layout_height="fill_parent"
    android:src="@drawable/opt2"
    android:scaleType="fitStart"
    android:id="@+id/option2"
    android:layout_width="fill_parent"
    android:longClickable="false" />
<ImageButton
    app:layout_widthPercent="40%"
    app:layout_heightPercent="40%"
    app:layout_marginLeftPercent="50%"
    app:layout_marginTopPercent="58%"
    android:background="#00ff0000"
    android:layout_height="fill_parent"
    android:src="@drawable/opt3"
    android:scaleType="fitStart"
    android:id="@+id/option3"
    android:layout_width="fill_parent"
    android:longClickable="false" />
<ImageButton
    app:layout_widthPercent="40%"
    app:layout_heightPercent="40%"
    app:layout_marginLeftPercent="50%"
    app:layout_marginTopPercent="21%"
    android:background="#00ff0000"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/option4"
    android:scaleType="fitCenter"
    android:id="@+id/opt4"
    android:longClickable="false" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Pick an Answer"
    android:id="@+id/pickLabel"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    app:layout_widthPercent="100%"
    app:layout_heightPercent="7%"
    app:layout_marginTopPercent="15%"
    android:textColor="#ffffff"
    android:textAlignment="center"
    android:textSize="30sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_widthPercent="100%"
    app:layout_heightPercent="12%"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="00"
    android:id="@+id/timeLabel"
    android:textAlignment="gravity"
    android:textSize="72sp"
    android:gravity="center|center_vertical|center_horizontal" />

您可以在外部布局上设置android:weightSum="1" ,在内部布局上设置android:layout_weight="0.25"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM