在具有嵌入式ScrollView的RelativeLayout中有点循环依赖

时间:2012-04-18 18:10:29

标签: android scrollview relativelayout

基本上,活动应显示在屏幕上:
1.顶部有一个标志。必须始终显示。当选择在屏幕上留出额外空间时,必须垂直和水平居中。
2.底部的ScrollView。托管垂直LinearLayout,它反过来托管用户可以选择的一组选项。必须占用显示所有选择所需的最小空间(因此徽标是垂直居中的)。可以占用屏幕上的(屏幕 - 徽标)空间。选择以编程方式添加。



现在,我已将其在布局中定义为:

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


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:padding="5dp"
            android:src="@drawable/logo" />

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_below="@id/imageView"
            >

            <LinearLayout
                android:id="@+id/ChoicesPanel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>
  1. 如果使用layout_above = scrollView布局ImageView,则ScrollView之前会进行布局,而不考虑ImageView的最小尺寸。
  2. 如果使用layout_below = ImageView布局ScrollView,ImageView就会保持在最顶层。当只有2-3个选择时,屏幕看起来很丑陋
    有没有办法可以满足约束条件?或者我应该定义两个不同的布局xml文件,并以编程方式切换到正确的文件?

2 个答案:

答案 0 :(得分:0)

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


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="5dp"
        android:src="@drawable/ic_launcher" />


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView" >

        <LinearLayout
            android:id="@+id/ChoicesPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_2"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_3"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_4"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_4"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_5"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_6"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_6"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_2"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_2"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_3"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_3"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_4"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_4"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_5"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_5"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_text_6"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="scroll_view_1_button_6"/>


        </LinearLayout>
    </ScrollView>
</RelativeLayout>

这似乎对我有用。它像滚动视图中的14个项目

答案 1 :(得分:0)

public class TestActivity extends Activity {

private RelativeLayout mRelativeLayout;
private RelativeLayout mRelativeLayout2;
private ImageView mImageView;
private ScrollView mScrollView;
private TextView TV1;
private TextView TV2;
private TextView TV3;
private TextView TV4;
private Button B1;
private Button B2;
private Button B3;
private Button B4;
private RelativeLayout.LayoutParams lp;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

mRelativeLayout = new RelativeLayout(this);
    setContentView(mRelativeLayout);

mImageView = new ImageView(this);
mImageView.setId(1);
RelativeLayout.LayoutParams lp = new      RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mImageView.setImageResource(R.drawable.ic_launcher);
mRelativeLayout.addView(mImageView, lp);

mScrollView = new ScrollView(this);
RelativeLayout.LayoutParams lp2 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, mImageView.getId());
mRelativeLayout.addView(mScrollView, lp2);

mRelativeLayout2 = new RelativeLayout(this);
int itemcounts = mRelativeLayout2.getChildCount();
mScrollView.addView(mRelativeLayout2);

TV1 = new TextView(this);
TV1.setId(2);
TV1.setTextSize(20);
TV1.setText("Im a Text View. The First");
mRelativeLayout2.addView(TV1);

B1 = new Button(this);
B1.setId(3);
RelativeLayout.LayoutParams bl1 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl1.addRule(RelativeLayout.BELOW, TV1.getId());
B1.setText("Im a Button. The First");
B1.setTextSize(25);
mRelativeLayout2.addView(B1, bl1);

TV2 = new TextView(this);
RelativeLayout.LayoutParams tvl2 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl2.addRule(RelativeLayout.BELOW, B1.getId());
TV2.setId(4);
TV2.setTextSize(20);
TV2.setText("Im a Text View. The Second");
mRelativeLayout2.addView(TV2, tvl2);

B2 = new Button(this);
B2.setId(5);
RelativeLayout.LayoutParams bl2 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl2.addRule(RelativeLayout.BELOW, TV2.getI());
B2.setText("Im a Button. The Second");
B2.setTextSize(25);
mRelativeLayout2.addView(B2, bl2);

TV3 = new TextView(this);
TV3.setId(6);
RelativeLayout.LayoutParams tvl3 = new      RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl3.addRule(RelativeLayout.BELOW, B2.getId());
TV3.setTextSize(20);
TV3.setText("Im a Text View. The Third");
mRelativeLayout2.addView(TV3, tvl3);

B3 = new Button(this);
B3.setId(7); 
RelativeLayout.LayoutParams bl3 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
bl3.addRule(RelativeLayout.BELOW, TV3.getId());
B3.setText("Im a Button. The Third");
B3.setTextSize(25);
mRelativeLayout2.addView(B3, bl3);

TV4 = new TextView(this);
TV4.setId(8);
RelativeLayout.LayoutParams tvl4 = new     RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
tvl4.addRule(RelativeLayout.BELOW, B3.getId());
TV4.setTextSize(20);
TV4.setText("Im a Text View. The Fourth");
mRelativeLayout2.addView(TV4, tvl4);


if (itemcounts < 4) {
    lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT);
}
if (itemcounts > 4) {
    lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
}

}
}

试试这个