0

好的,所以我决定参加 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>
4

1 回答 1

1

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

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

于 2012-08-30T19:03:20.603 回答