【问题标题】:Android Studio Layout: Trying to make button 50% width and 50% height, with button in centerAndroid Studio 布局:尝试使按钮的宽度为 50%,高度为 50%,按钮位于中心
【发布时间】:2016-01-09 04:35:10
【问题描述】:

我在 Android Studio 中工作,希望我的布局是一个 2 x 2 的按钮网格,中间有一个按钮。请参阅下面的代码。

<RelativeLayout 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:id="@+id/incentivebutton">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Incentive"
    android:id="@+id/incentiveButton"
    android:onClick="IncentiveButton"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:nestedScrollingEnabled="true"
    android:background = "@drawable/roundbutton"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="55dp"
    android:paddingBottom="55dp"
    android:layout_gravity="center_vertical" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="6dp"
    android:weightSum="2"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
        <Button
            android:id="@+id/buttonCollect"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_weight="1"/>

        <Button
            android:id="@+id/buttonCollect1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_weight="1">
        <Button
            android:id="@+id/buttonCollect3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/buttonCollect2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

这就是现在的样子:

enter image description here

我喜欢这种布局,但我希望按钮的高度为 50%。有人有什么建议吗?

【问题讨论】:

    标签: android xml android-layout android-studio android-linearlayout


    【解决方案1】:

    如果您正在寻找这样的设计

    您可以使用FrameLayout

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="2" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="2" >
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="1" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="2" />
            </LinearLayout>
    
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:weightSum="2" >
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="3" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="4" />
            </LinearLayout>
        </LinearLayout>
    
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/instagram"/>
    
    </FrameLayout>
    

    我已将LinearLayout 与weightSum 一起使用。看看有没有帮助!

    【讨论】:

      猜你喜欢
      • 2013-04-17
      • 1970-01-01
      • 2015-06-24
      • 2021-09-26
      • 2014-11-23
      • 2023-03-23
      • 1970-01-01
      • 2019-03-03
      • 2016-05-18
      相关资源
      最近更新 更多