简体   繁体   English

带自定义按钮的Android布局

[英]Android Layout with custom button

I am trying to get the button to be in the center of screen and a logout button at the bottom. 我正在尝试使按钮位于屏幕的中心,并在底部显示一个注销按钮。 I have the button centered vertically but it won't center horizontally. 我的按钮垂直居中,但不会水平居中。

What am I do wrong? 我做错了什么?

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

<TextView
    android:layout_width="match_parent"
    android:textColor="#000000"
    android:id="@+id/etEmailLabel"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="center"
    android:textAlignment="center" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_marginBottom="20dp"
    android:weightSum="1"
    android:layout_weight="0.98">

    <Button
        android:layout_width="291dp"
        android:layout_height="273dp"
        android:id="@+id/DataLog"
        android:background="@layout/mybutton"
        android:text="Log Location"
        android:layout_gravity="center" />

</LinearLayout>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bLogout"
    android:text="Logout"/>

</LinearLayout>

Custom Button Code: 自定义按钮代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:layout_gravity="center"
    android:shape="oval">
    <solid android:color="#FFFFFF"/>
    <stroke android:width="2sp" android:color="#fff" />
</shape>

I'm confused as to why your LinearLayout height is smaller than the button, but you can center it by applying gravity to the parent ViewGroup : 我对为什么LinearLayout高度小于按钮感到困惑,但是您可以通过将重力应用于父ViewGroup来居中:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="150dp"
    android:layout_marginBottom="20dp"
    android:weightSum="1"
    android:gravity="center"
    android:layout_weight="0.98">

    <Button
        android:layout_width="291dp"
        android:layout_height="273dp"
        android:id="@+id/DataLog"
        android:background="@layout/mybutton"
        android:text="Log Location" />

</LinearLayout>

Also, the weighting and weightSum on your LinearLayout will not have any affect. 另外, LinearLayout上的weighting和weightSum不会有任何影响。

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

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