ScrollView不会在AlertDialog中滚动

时间:2014-06-30 10:53:22

标签: android scrollview android-linearlayout

为什么scrollView不在AlertDialog中滚动?

AlertDialog.Builder popupBuilder = new AlertDialog.Builder(this);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
sv.addView(ll);
for (int i=0;i<p.arr.size();i++){
    addPhoto(ll,p.arr.get(i));
}
popupBuilder.setView(sv);
popupBuilder.show();

因此,addPhoto()会将数组arr中的照片添加到LinearLayout 'll'

1 个答案:

答案 0 :(得分:0)

这是XML中的布局:

<ScrollView>
    <LinearLayout android:orientation="vertical"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true">
        <TextView />
        <Button />
    </LinearLayout>
</ScrollView>

so以与所需视图相同的方式动态创建视图

示例2

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textmsg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/hello" />

</LinearLayout>

在活动类

LayoutInflater inflater= LayoutInflater.from(this);
View view=inflater.inflate(R.layout.yourxmlfile, null);

TextView textview=(TextView)view.findViewById(R.id.textmsg);
textview.setText("Your really long message.");
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
alertDialog.setTitle("Title");  
//alertDialog.setMessage("Here is a really long message.");
alertDialog.setView(view);
alertDialog.setButton("OK", null);  
AlertDialog alert = alertDialog.create();
alert.show();

Note ScrollView容器只能有一个子布局视图。例如,在没有LinearLayout的情况下将TextView和Button放在ScrollView中是不可能的。