尝试在Android中的RelativeLayout末尾添加按钮

时间:2016-03-06 01:23:48

标签: java android android-layout

嗨=)我正在尝试在Android中开发一个应用程序,它是一个基本的文本冒险游戏,它显示一些文本,然后有几个按钮供您选择的选项。非常非常基本。然而,文本进入TextView,每次按下按钮时动态更改TextView以生成故事的下一部分。我的按钮也是动态生成的。我认为由于这个原因,它们不会在布局树中显示的TextView下生成。无论如何,这是我的代码。我的问题主要是如何使按钮显示在可滚动TextView的底部,与顶部相比?其次,如果有人可以让我有这个信息,我如何清除按钮布局,以便我可以创建更多的按钮?如果其中任何一个被问到这将是非常有帮助的。谢谢! ^ - ^

爪哇:

package toymakersdev.com.theinsane;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class GameActivity extends AppCompatActivity {

private TextView storyText;
private LinearLayout buttonLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    buttonLayout = (LinearLayout)findViewById(R.id.buttonLayout);

    storyText = (TextView)findViewById(R.id.storyText);
    storyText.setMovementMethod(new ScrollingMovementMethod());

    storyText.setText("Blah Blah Blah");

    Button button1 = new Button(this);
    Button button2 = new Button(this);
    button1.setText("Button1");
    button2.setText("Button2");

    button1.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    button2.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));

    buttonLayout.addView(button1);
    buttonLayout.addView(button2);
}
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<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="toymakersdev.com.theinsane.GameActivity"
android:background="#000000"
android:padding="10dp">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/scrollView"
    android:padding="10dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/storyText"
            android:textColor="#ffffff"
            android:textAlignment="center"
            android:textSize="32sp"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/buttonLayout">
        </LinearLayout>

    </RelativeLayout>
</ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

  

与顶部相比,如何使按钮显示在可滚动TextView的底部?

使用

android:layout_alignParentBottom="true"

并在ScrollView中添加此行

android:layout_above="@+id/button"

修改布局如图所示

<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="toymakersdev.com.theinsane.GameActivity"
android:background="#000000"
android:padding="10dp">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@+id/button"
android:id="@+id/scrollView"
android:padding="10dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/storyText"
        android:textColor="#ffffff"
        android:textAlignment="center"
        android:textSize="32sp"/>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/buttonLayout">
    </LinearLayout>

</RelativeLayout>
</ScrollView>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"/>

</RelativeLayout>
  

如何清除按钮布局以便我可以创建更多按钮?

您可以使用removeView()方法从其父级删除按钮视图。像例子

((ViewGroup)button.getParent()).removeView(button);