【问题标题】:How can I place another button on top of four buttons in Android Studio如何在 Android Studio 中的四个按钮之上放置另一个按钮
【发布时间】:2018-05-05 11:34:05
【问题描述】:

我正在尝试在我的 android 应用程序中实现这种格式。格式如下:-

[]

<LinearLayout 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"
android:padding="20dp"
tools:context="com.example.nero.application.MainControl">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp">

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="Button"
        android:textSize="20dp"
        android:textColor="#f000"
        android:background="@color/red"/>

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="Button"
        android:textSize="20dp"
        android:textColor="#f000"
        android:background="@color/red" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="10dp">

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="Button"
        android:textSize="20dp"
        android:textColor="#f000"
        android:background="@color/red"/>

    <Button
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:text="Button"
        android:textSize="20dp"
        android:textColor="#f000"
        android:background="@color/red"/>

</LinearLayout>

我总是在我的工作中使用 LinearLayout,因为它易于理解并且在堆栈中工作。然而,RelativeLayout 对我来说有点复杂(个人)。这是我目前所做的,我还尝试在所有内容之上添加一个 FrameLayout 以显示最终按钮,但我无法获得准确的定位。

我试图避免的另一个问题是使这种设计适用于不同的屏幕尺寸。

任何帮助将不胜感激,谢谢!

【问题讨论】:

  • 加完FrameLayout后能贴出代码吗?
  • 对不起,伙计,我已经删除了 FrameLayout 代码,因为当我将 LinearLayout 放在 FrameLayout 中时,所有按钮都放在彼此的顶部。
  • 这里你应该做的:把整个代码保持原样(甚至保持外部的LinearLayout)并将它放在FrameLayout中,确保视觉上没有任何变化。然后在 FrameLayout 内部但在 LinearLayout 外部添加一个按钮,并为 FrameLayout 提供重力中心。

标签: android xml button layout


【解决方案1】:

这应该可行:

<FrameLayout
    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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:padding="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="10dp">

            <Button
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:text="Button"
                android:textSize="20dp"
                android:textColor="#f000"
                android:background="@color/red"/>

            <Button
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:text="Button"
                android:textSize="20dp"
                android:textColor="#f000"
                android:background="@color/red" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:padding="10dp">

            <Button
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:text="Button"
                android:textSize="20dp"
                android:textColor="#f000"
                android:background="@color/red"/>

            <Button
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:text="Button"
                android:textSize="20dp"
                android:textColor="#f000"
                android:background="@color/red"/>

        </LinearLayout>
    </LinearLayout>

    <View android:layout_height="150dp"
        android:layout_width="150dp"
        android:background="#54e"
        android:layout_gravity="center"/>
</FrameLayout>

只需将 View 元素替换为您需要的任何类型,然后添加椭圆形背景以使其变为圆形。

【讨论】:

  • 像魅力一样工作,也很有意义谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 2017-03-15
相关资源
最近更新 更多