【问题标题】:Selecting best Android layout选择最佳 Android 布局
【发布时间】:2026-01-08 22:00:02
【问题描述】:

我是 Android 新手,希望做如下布局:

  1. 顶部有一个徽标。
  2. 跟随圆角矩形
  3. 在该矩形内,我将有两个用于用户 ID 和密码的 EditText 框,以及一个登录按钮
  4. 在圆角矩形下方(外部)我有一个指向条款和条件的 Html 链接

我尝试了各种布局方式

  1. 仅使用布局。不同种类的布局。一切似乎都很难实现我所需要的
  2. 使用布局 + 背景。背景并不是真正的背景,而是更像一个模板,它会影响你的布局,并且很难控制你想要控制的位置。
  3. 使用 onDraw。灵活但担心不同的屏幕尺寸可能会出现问题。

那么,请有人指出实现我需要的最佳方法是什么?

【问题讨论】:

  • 使用 XML,我认为使用与主视图一样垂直的线性布局,并在第一行嵌入徽标,在第二行嵌入相对布局,这样你就可以做出你想要的。当然,我相信你会为圆角矩形制作自己的资产

标签: android layout


【解决方案1】:

没有人能真正告诉你什么是最好的,这取决于你想要什么,但我建议使用RelatvieLayout,因为一旦你稍微使用它们,它们通常是最简单和最有效的,在我的观点。您可以阅读Here 了解如何制作矩形。你基本上会使用shape drawable 并调整cornersradius

至于顶部的徽标,如果它会在其他Activitys 中重复使用,那么您可以将其放在自己的布局中,并在您的布局中使用include 标签来重复使用徽标布局

如果您担心不同的屏幕尺寸,请阅读Docs 并找到适合您的。 只需从它开始并随时调整。不要害怕搞砸并重做一些。希望这些信息足以帮助您入门

使用RelativeLayout 将为您提供更大的灵活性,并允许您使用更少的Layouts,例如嵌套LinearLayouts 和Layouts,只有一个孩子可以提高性能

【讨论】:

    【解决方案2】:

    应该这样做: 从垂直方向的线性布局开始:

    <linearLayourt xmlns=............
    android:orientation="vertical"
    .....other stuffs goes here
    ......
    .....
    <LinearLayout ......this is the child linearlayout
    .....other stuffs goes here like width and height
    <ImageView ...this is where you are gonna put your logo in
    />
    </LinearLayout> ....close your child linear layout
    <RelativeLayout ...
    .........other stuffs here
    <EditText ....1st edit text
    ...you position your boxes here
    />
    <EditText ....2nd edit text
    ...you position your boxes here
    />
    </RelativeLayout>
    <TextView 
    ....
    ...
    ...put yout hyperlink for this text
    />
    </LinearLayout> ...this is the parent linear layout
    

    【讨论】:

    • 谢谢。有用。不幸的是,我还不能按下投票按钮。
    • 如果您可以标记为正确答案,那就太好了。谢谢
    【解决方案3】:

    对于您创建登录屏幕的情况,这并不重要,因为它是一个相对容易设计的屏幕。我个人喜欢使用 XML 来设计我的布局,但从未见过使用 onDraw 方法完成它。

    正如@codeMagic 所说,我对您的建议是学习如何使用和操作RelativeLayouts,因为这会阻止您创建真正不推荐并且需要很长时间才能加载的级联布局。

    当我开始为 Android 编程时,我发现LinearLayout 是最容易理解和使用的,但使用它会让我在复杂的屏幕设计中看到LinearLayouts 中的许多LinearLayouts,后来使用RelativeLayout 我意识到在大多数情况下,一个RelativeLayout 可以替换许多级联的线性布局。

    在你的情况下,你可以做这样的事情:

     <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
          <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/drop_down_icon" />
    
       <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/imageView1" >
    
        </EditText>
    
        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/editText1" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/editText2"
            android:layout_centerHorizontal="true"
            android:text="Button" />
    
    
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/button1"
            android:layout_centerHorizontal="true"
            android:text="TextView" />
    
    </RelativeLayout>
    

    剩下的就是添加所需的填充和边距。

    【讨论】: