【问题标题】:How to arrange views on the screen?如何在屏幕上排列视图?
【发布时间】:2013-12-27 03:11:28
【问题描述】:

我的屏幕上有一个按钮、一个 textView 和一个 scrollView。我需要 button 和 textView 在顶部彼此并排,scrollView 在它们下方。如何专业地在屏幕上排列视图?!

这就是我所做的:

<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:textDirection="inherit"
tools:context=".MainActivity" >

<Button
    android:id="@+id/btnScanAndDraw"
    android:layout_width="wrap_content"
    android:layout_height="35dp"
    android:text="@string/btnShoePosition"
    android:textSize="12sp" 
    android:onClick="ScanAndDraw"/>

<TextView
    android:id="@+id/txvMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="end">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/imgViewMap"/>
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 第一步:尝试一下。
  • 所以我在顶部做了一个线性布局。但他们都并肩作战。我尝试了视图的 margin_top 属性,但只是想知道这是最好的方法还是有其他方法!
  • 发布一些带有布局外观截图的代码会更容易。
  • 好的@mikeaworski 我做到了:)
  • 现在人们可以将该代码复制到自己的 xml 中并自行操作。布局的屏幕截图也不错,但这不是必需的。

标签: android layout view


【解决方案1】:

不确定你到底想要什么。如果您要问与布局相关的问题,也请发布屏幕截图。如果我理解您的问题,解决方案如下。

您正在使用LinearLayout,它只有两个方向(垂直和水平)。 Click here 了解LinearLayout。如果您想以相对的方式排列您的 UI,请使用 RelativeLayoutRelativeLayout。我已经用一些新属性更改了您的代码。它没有经过测试,但它应该会给你想要的输出。

<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"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/txvMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"/>

    <Button
        android:id="@+id/btnScanAndDraw"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="@string/btnShoePosition"
        android:textSize="12sp" 
        android:onClick="ScanAndDraw"
        android:layout_toRightOf="@+id/txvMain/>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below = "@+id/txvMain
        />

        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:id="@+id/imageView"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:contentDescription="@string/imgViewMap"/>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

【讨论】:

  • 它会做出改变,谢谢,但它会覆盖我的按钮和文本视图。我只想将scrollView放在屏幕上的按钮和文本视图下。
  • 我将线性布局的方向更改为垂直。现在它们都垂直排列在彼此下方。你知道怎么只把imageView放在另外两个下面吗?
【解决方案2】:

我为 Android 应用程序开发编写了一本书籍。阅读这本书后,我意识到可以有几个 LinearLayout 会相互进入。使用 LinearLayout 的这种层次结构,我可以在 .XML 文件中排列视图。例如,我安排了一个根 LinearLayout,它本身包含 3 个以上的 LinearLayout。对于顶部的按钮,我写道:

android:gravity="right"

然后按钮转到顶部。对于 ImageView - 在按钮下 - 我实现了另一个 LinearLayout,其中包含这行代码以将 ImageView 水平居中:

android:gravity="center_horizontal"

在这种形式中 - 正如我所说 - 使用内部 LinearLayouts 和重力可以按预期在屏幕上排列视图。

可能对某人有用:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多