Android - 如何使此警报对话框可滚动?

ash*_*shu 18 android android-alertdialog

我是android的初学者,并制作我的第一个Android应用程序.点击后,我的"关于"菜单项会显示一条带有非常长消息的alertdialog.我一直在尝试不同的方法使其可滚动,但我不能.我曾尝试在stackoverflow上阅读不同的问题,但它们对我没有用.这是我的警报对话框代码.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);  
alertDialog.setTitle("Title");  
alertDialog.setMessage("Here is a really long message.");  
 alertDialog.setButton("OK", null);  
 AlertDialog alert = alertDialog.create();
alert.show();
Run Code Online (Sandbox Code Playgroud)

任何人都可以详细解释我如何使其可滚动?任何帮助或建议将不胜感激!

blg*_*101 26

这个解决方案来自这篇文章.

为了使视图可滚动,它必须嵌套在ScrollView容器中:

<ScrollView>
    <LinearLayout android:orientation="vertical"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true">
        <TextView />
        <Button />
    </LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

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


Muk*_*ngh 8

在这种情况下,您可以在Scroll View下创建自己的包含文本视图的layout.xml文件.并在此文本视图中设置TextMessage,使用警报对话框为此布局充气.

yourxmlfile.xml

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <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>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

在活动类中

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();
Run Code Online (Sandbox Code Playgroud)