0

基本上,一个活动应该显示在屏幕上:
1. 顶部的标志。必须始终显示。Must center both vertically and horizo​​ntally, when choices leaves extra space on screen.
2.底部有一个ScrollView。托管垂直线性布局,后者又托管一组用户可以选择的选项。必须占用最少的空间来显示所有选项(因此徽标垂直居中)。最多可占用屏幕上的 (Screen - Logo) 空间。以编程方式添加选项。



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

    <?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. 如果 ImageView 使用 layout_above=scrollView 布局,则 ScrollView 在之前布局,不考虑 ImageView 的最小尺寸。
  2. 如果使用 layout_below=ImageView 对 ScrollView 进行布局,则 ImageView 将停留在顶部。当只有 2-3 个选项时,屏幕看起来很丑。

    有没有办法可以满足约束?或者我应该定义两个不同的布局 xml 文件,并以编程方式切换到正确的一个?
4

2 回答 2

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 个项目

于 2012-04-18T19:06:47.590 回答
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);
}

}
}

尝试这个

于 2012-04-19T18:29:24.907 回答