【问题标题】:placing TextView, EditText and Button in Android Layout xml file在 Android Layout xml 文件中放置 TextView、EditText 和 Button
【发布时间】:2014-04-08 23:56:35
【问题描述】:

我是 android 编程新手,我正在尝试了解 android 架构以及应用程序是如何围绕它构建的。

因此,目前还没有现实世界对此的需求。我正在做一些实验来学习这些东西。我想要的是 3 个不同的视图,TextView、EditText 和 Button,它们水平相邻。为了实现这一点,这是我正在使用的 activity_main.xml:-

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

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

  <EditText android:id="@+id/edit_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text_to_appear_on_button" />

</LinearLayout>

在运行具有 setContentView(R.layout.activity_main); 的 MainActivity.java 时,在 onCreate() 中,我在屏幕上获得了 TextView 和 EditText 小部件,它们水平相邻,但没有 Button .我想知道为什么?

奇怪的是,我观察到里面的最后一个元素 &lt;LinearLayout&gt;..&lt;/LinearLayout&gt; 是从屏幕上消失的那个。因此,如果&lt;Button .. /&gt; 与&lt;TextView .. /&gt; 交换,那么它的&lt;TextView&gt; 元素现在将不会在屏幕上可见。

请解释一下我在这里遗漏了什么。

我正在模拟器上运行 MainActivity.java 并使用 Eclipse 作为我的 IDE,如果这些信息有帮助的话。

【问题讨论】:

标签: android android-layout android-view android-xml


【解决方案1】:

这取决于你想做什么。如果您想在LinearLayout 中水平放置三个东西,您可能会用完屏幕上的空间。为确保所有三个都适合,请设置:

android:layout_width="match_parent"
android:layout_weight="1"

对于所有 3。您可以随意调整权重,但基本上这会告诉渲染以水平适应屏幕上的所有三个对象,每个对象占据屏幕的 1/3(如果您更改重量,它将是不同的值)。

如果使用LinearLayout,您可能会嵌套多个布局,主垂直LinearLayout 包含多个水平布局。这是一种有效的方法,并且可能是一个偏好问题。 LinearLayout 允许使用权重,这非常有用,因为它们是确保事物不会被屏幕截断的一种方式。

RelativeLayout 是另一种方法,您可以指定屏幕上的事物相对于彼此的位置(左、右、上、下)。虽然这些不使用权重,但您可以将元素与屏幕边缘对齐并获得相同的效果。

正如我所说,这种方法在很大程度上是一个偏好问题,通常两者的某些网格效果都很好。

【讨论】:

  • 感谢您的回复。这很有用。
【解决方案2】:

我建议您为您的 xml 使用相对布局,如果您使用线性,则您的小部件会被一一分配,而不是您的愿望。它供您进一步开发

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="101dp"
            android:layout_marginTop="50dp"
            android:text="TextView" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="67dp"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/editText1"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="59dp"
            android:text="Button" />

    </RelativeLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2011-04-04
    • 2014-02-18
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多