如何在android上设置屏幕中间的按钮

时间:2018-06-06 06:13:11

标签: android

我在Android应用程序中定位我的按钮时遇到了一些问题。基本上,如果你看下面的图像

我有三个按钮。我希望按钮按原样放置,除了屏幕中间显示的“播放器2”按钮。

我尝试设置layout_parentcenter,但这没有任何影响,我不想通过边距来做,因为当我尝试这个时,当我将手机旋转到水平时,按钮不在正确的位置。

下面是当前按钮布局的图像:

enter image description here

以下是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@drawable/brick_wall">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title "
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        app:fontFamily="@font/balloon_extra_bold"
        android:textSize="50sp"
        android:textColor="@color/colorPrimaryLight" />

        <Button
            android:id="@+id/button_players1"
            android:layout_width="143dp"
            android:layout_height="30dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/app_buttons"
            android:fontFamily="@font/balloon_extra_bold"
            android:gravity="center"
            android:text="1 Player"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:layout_below="@id/title"/>

    <Button
        android:id="@+id/button_players2"
        android:layout_width="145dp"
        android:layout_height="30dp"
        android:layout_below="@id/button_players1"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/app_buttons"
        android:fontFamily="@font/balloon_extra_bold"
        android:gravity="center"
        android:text="2 Players"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>


    <Button
        android:id="@+id/button_how_to_play"
        android:layout_width="148dp"
        android:layout_height="30dp"
        android:layout_below="@id/button_players2"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/app_buttons"
        android:fontFamily="@font/balloon_extra_bold"
        android:gravity="center"
        android:text="How to Play"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:layout_marginTop="15dp"/>

    <ImageView
        android:id="@+id/footer_logo"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@drawable/logo" />

</RelativeLayout>

5 个答案:

答案 0 :(得分:2)

将button_players2置于垂直中心,并将button_players1置于button_players2上方。
button_players1,button_players2和button_how_to_play将位于屏幕的中心 尝试粘贴下面的代码。

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/brick_wall"
    android:orientation="vertical">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Title "
        android:textColor="@color/colorPrimaryLight"
        android:textSize="50sp"
        app:fontFamily="@font/balloon_extra_bold" />

    <Button
        android:id="@+id/button_players1"
        android:layout_width="143dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/button_players2"
        android:background="@drawable/app_buttons"
        android:fontFamily="@font/balloon_extra_bold"
        android:gravity="center"
        android:text="1 Player"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <Button
        android:id="@+id/button_players2"
        android:layout_width="145dp"
        android:layout_height="30dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/app_buttons"
        android:fontFamily="@font/balloon_extra_bold"
        android:gravity="center"
        android:text="2 Players"
        android:textColor="@color/black"
        android:textSize="20sp" />


    <Button
        android:id="@+id/button_how_to_play"
        android:layout_width="148dp"
        android:layout_height="30dp"
        android:layout_below="@id/button_players2"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/app_buttons"
        android:fontFamily="@font/balloon_extra_bold"
        android:gravity="center"
        android:text="How to Play"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/footer_logo"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp"
        android:src="@mipmap/logo" />

</RelativeLayout>

答案 1 :(得分:0)

您已经在使用layout_centerHorizo​​ntal。另一个方向的是layout_centerVertical。您可能希望将所有按钮放在线性布局中并使布局居中,而不是将按钮居中(否则3将布置在彼此之上)。

它现在不能为你工作的原因(你在底部的2个按钮上确实有layout_centerVertical)是你还要对它们施加layout_below约束,而那些正在覆盖它。不要使用两者 - 而是使用我建议的LinearLayout或在其他按钮上使用layout_above。

答案 2 :(得分:0)

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:text="Title "
            android:textSize="50sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:layout_centerInParent="true">



            <Button
                android:id="@+id/button_players1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:text="1 Player"
                android:textSize="20sp" />

            <Button
                android:id="@+id/button_players2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button_players1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="15dp"
                android:gravity="center"
                android:text="2 Players"
                android:textSize="20sp" />


            <Button
                android:id="@+id/button_how_to_play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button_players2"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginTop="15dp"
                android:gravity="center"
                android:text="How to Play"
                android:textSize="20sp" />

            <ImageView
                android:id="@+id/footer_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="20dp" />

        </LinearLayout>

    </RelativeLayout>

答案 3 :(得分:0)

尝试以下方法:

localhost/Resolusi2018/root/php/ngetes.php

答案 4 :(得分:0)

你可以采取一组按钮并将其包裹在relativelayout中,并使相对布局为父中心或垂直和水平中心

props