带ScrollView的Android自定义对话框

时间:2015-04-12 09:56:32

标签: android android-layout

我的对话框显示尺寸问题。使用下面的代码,我在里面使用ScrollView创建自定义对话框,但是当它显示时,ScrollView适合对话框尺寸,但内部线性布局有一些可滚动区域的控件。 我无法理解为什么以及如何解决。

在没有显示器的手机中,这是一个问题,因为某些控件无法访问。

创建对话框:

AlertDialog.Builder alert = new AlertDialog.Builder(DataMainPage.this);                     
startNewTestSessionDialog = new  StartTestDialog(DataMainPage.this);
                     alert.setView(startNewTestSessionDialog);
alert.setPositiveButton("Start", new DialogInterface.OnClickListener() {<...>});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {<..>});

Dialog d = alert.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
d.show();
d.getWindow().setAttributes(lp);

对话框视图xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sample="http://schemas.android.com/apk/res/ips.tecnos.saildata"
android:id="@+id/startTest_dialogView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="65dp"
android:fillViewport="true" >

<LinearLayout
    android:id="@+id/startTest_viewContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clipToPadding="false"
    android:orientation="vertical" >

    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:orientation="horizontal"
     android:weightSum="2"
     android:layout_marginBottom="5dp" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:layout_marginRight="7dp" 
            android:gravity="right|center_vertical"
            android:text="@string/testStart_disableWind"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <CheckBox
            android:id="@+id/startTest_disableWind"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="" />

    </LinearLayout>

     <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:layout_marginTop="10dp"
            android:text="@string/testStart_boatType"
            android:textAppearance="?android:attr/textAppearanceMedium" />

      <LinearLayout 
         android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_marginBottom="5dp" >

        <Spinner
            android:id="@+id/startTest_boatSelect"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center|right"
            android:layout_weight="2"
            android:gravity="center|right" />

        <Button
            android:id="@+id/startTest_modifyBoatPolar"
            android:layout_width="0dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/testStart_modboatPolar" />

    </LinearLayout>

   <...other controls with same logic...>

</LinearLayout>

</ScrollView>

Screenshoot(Nexus5):ScrollView即将结束,正如您所见,按钮和隐藏的微调器不在scrollView边框之内。 Screnshoot

1 个答案:

答案 0 :(得分:0)

随着一些随意的变化,我找到了解决方案!

问题是ScrollView的属性:

android:layout_gravity="center"

删除它,布局正确。