RelativeLayout Прокручиваемый
Я много раз пытался переопределить элементы в RelativeLayout, но я не получил результат, который я ожидал:
![enter image description here]()
Я хотел бы выровнять первую кнопку вверху справа, а затем у меня есть несколько Textview, и я хочу, чтобы все они прокручивались, но когда я вставляю тег, я получаю сообщение об ошибке, это мой код:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RL01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btnBrowser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Browser"
android:layout_marginTop="5dp"
>
</Button>
<TextView
android:id="@+id/txtAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:textColor="#143781"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/btnBrowser"
>
</TextView>
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textColor="#8D89B3"
android:layout_marginTop="5dp"
android:layout_marginLeft="20dp"
android:layout_below="@+id/txtAuthor">
</TextView>
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:layout_below="@+id/txtDate">
>
</TextView>
<TextView
android:id="@+id/txtMsg"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="14dp"
android:textColor="#000000"
android:layout_marginLeft="20dp"
android:layout_below="@+id/txtTitle">
</TextView>
<Button
android:id="@+id/btnReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reply"
android:layout_marginTop="20dp"
android:layout_below="@+id/txtMsg"
android:layout_centerInParent="@+id/txtMsg">
</Button>
</ScrollView>
</RelativeLayout>
Ответы
Ответ 1
ScrollView
не позволяет больше одного прямого дочернего элемента, поэтому вы получаете ошибку. Поэтому вы пытаетесь сделать следующее:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:text="TextView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView3"
android:text="Button" />
</RelativeLayout>
</ScrollView>
Ответ 2
Вы не можете добавить несколько виджета в scrollview. Для этого вы должны обернуть виджет с представлением. как ниже
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnBrowser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="false"
android:layout_marginTop="5dp"
android:text="Browser" >
</Button>
<TextView
android:id="@+id/txtAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnBrowser"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Text"
android:textColor="#143781"
android:textSize="20dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtAuthor"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:textColor="#8D89B3"
android:textSize="14dp" android:text="Text"
>
</TextView>
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtDate"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:textColor="#000000"
android:textSize="14dp"
android:textStyle="bold" android:text="Text">
</TextView>
<TextView
android:id="@+id/txtMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_marginLeft="20dp"
android:textColor="#000000"
android:textSize="14dp" android:text="Text">
</TextView>
<Button
android:id="@+id/btnReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtMsg"
android:layout_centerInParent="@+id/txtMsg"
android:layout_marginTop="20dp"
android:text="Reply" >
</Button>
</RelativeLayout>
</ScrollView>
Ответ 3
Вы добавляете scrollview
внутрь relativelayout
. Вы должны добавить relativelayout
внутри scrollview
, чтобы получить результаты. и еще одна вещь scrollview
может содержать только одного ребенка за раз, поэтому добавьте relativelayout
в scrollview
и посмотрите разницу