Android输入框在下一行

时间:2012-08-30 17:14:04

标签: android-layout

好的,所以我决定参加Google课程来学习droid devving。我决定使用简单的输入框和按钮来制作多个输入框和按钮,并且每个都显示一组颜色。问题是它是在一条线上干扰所有输入框,而不是它们自己的单独线。这就是我在activity_color.xml

中的内容
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal" >

<EditText android:id="@+id/edit_message_blue"
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:hint="@string/edit_message_blue" />


<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send_blue" 
    android:onClick="sendMessageBlue" />

<EditText android:id="@+id/edit_message_orange"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:hint="@string/edit_message_orange" />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/button_send_orange" 
    android:onClick="sendMessageOrange" />

</LinearLayout>

这是主要的java类

package com.example.color.texts;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class ColorTexts extends Activity {
public final static String EXTRA_MESSAGE = "com.example.colortexts.MESSAGE";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_color_texts);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_color_texts, menu);
    return true;
}

/** Called when the user clicks the Send button for Blue */
public void sendMessageBlue(View view) {
    Intent intent = new Intent (this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message_blue);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}

/** Called when the user clicks the Send button for Red */
public void sendMessageRed(View view) {
    Intent intent = new Intent (this, DisplayMessageActivityRed.class);
    EditText editText = (EditText) findViewById(R.id.edit_message_red);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}


/** Called when the user clicks the Send button for Orange */
public void sendMessageOrange(View view) {
    Intent intent = new Intent (this, DisplayMessageActivityOrange.class);
    EditText editText = (EditText) findViewById(R.id.edit_message_orange);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

}
}

现在我想问题是如何告诉它开始一个新的行,比如android:layout_next =“1”或者什么(我知道不存在),或者我是否必须添加到公共类?比如制作另一个布局文件并引用那个?我非常怀疑后者会起作用。

编辑:根据布置的说明,我认为这是我应该这样做的,但它只知道显示第一行:(

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText android:id="@+id/edit_message_blue"
    android:layout_width="0dp"
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:hint="@string/edit_message_blue" />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send_blue" 
    android:onClick="sendMessageBlue" />

</LinearLayout>

<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:orientation="horizontal" >    

<EditText android:id="@+id/edit_message_red"
    android:layout_width="0dp" 
    android:layout_height="wrap_content"
    android:layout_weight="1" 
    android:hint="@string/edit_message_red" />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/button_send_red" 
    android:onClick="sendMessageRed" />

</LinearLayout>

<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:orientation="horizontal" >

<EditText android:id="@+id/edit_message_orange"
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:hint="@string/edit_message_orange" />

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/button_send_orange" 
    android:onClick="sendMessageOrange" />

</LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

您的父布局设置为android:orientation="horizontal"。这需要设置为android:orientation="vertical"

编辑:以下是Android文档中的参考: http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:orientation