【问题标题】:Android Layout obstacleAndroid 布局障碍
【发布时间】:2011-03-22 19:30:53
【问题描述】:

大家好,

我正在开发一个 android 应用程序。在主屏幕上,我有 6 个按钮。

像这样。在按钮上有一个显示当前日期的透明视图。

问题:

  1. 我无法设计相同的布局。
  2. 我不知道如何在同一个窗口上重叠两个视图。
  3. 如果我制作了六个按钮,然后如果我采用另一个布局,则背景布局会被隐藏并且根本不会显示。

我知道这件事是我们需要两个视图在一个视图上我可以显示 6 个按钮,在另一个视图上我可以显示日期。但是怎么做?

提前致谢

【问题讨论】:

    标签: android


    【解决方案1】:

    这里的关键是FrameLayoutFrameLayout 的子元素垂直堆叠在一起。

    类似

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <!-- The relative layout contains all of your buttons -->
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
    
            <Button
                android:id="@+id/button1"
                android:text="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
    
            <Button
                android:id="@+id/button2"
                android:text="2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/button1"
                />
    
            <Button
                android:id="@+id/button3"
                android:text="3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button1"
                />
    
            <Button
                android:id="@+id/button4"
                android:text="4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button2"
                android:layout_toRightOf="@id/button3"
                />
    
            <Button
                android:id="@+id/button5"
                android:text="5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button3"
                />
    
            <Button
                android:id="@+id/button6"
                android:text="6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button4"
                android:layout_toRightOf="@id/button5"
                />
    
        </RelativeLayout>
    
        <!-- Your overlay -->
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:id="@+id/overlayText"
                android:text="Overlay"
                />
    
        </RelativeLayout>
    
    </FrameLayout>
    

    如果您无法使用单个 RelativeLayout 来对齐按钮,您可能需要使用一些嵌套的 LinearLayouts(尽管尽量避免使用)。

    【讨论】:

      【解决方案2】:

      检查此布局,它一定可以帮助您解决问题

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <RelativeLayout
          android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_alignParentLeft="true"></Button>
        <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:layout_alignParentLeft="true"></Button>
         <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"></Button>
         <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_alignParentRight="true"></Button>
          <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:layout_alignParentRight="true"></Button>
          <Button
         android:text="Hello"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_alignParentRight="true"></Button>
        </RelativeLayout>
        <ImageView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
      
              android:src="@drawable/icon"/>
        </FrameLayout>
      

      要使图像透明,请参阅this

      【讨论】:

        【解决方案3】:

        据我了解,您需要将一个视图“叠加”在另一个视图之上?

        为此,您需要合并视图并使顶视图透明。看看这篇文章... Layout Tricks: Merging Layouts

        【讨论】:

          猜你喜欢
          • 2017-10-01
          • 2019-05-06
          • 1970-01-01
          • 1970-01-01
          • 2023-01-28
          • 1970-01-01
          • 2021-07-15
          • 2021-09-05
          • 2019-07-02
          相关资源
          最近更新 更多