我是一个android的初学者,并制作了我的第一个android应用。当点击我的“关于”菜单项时,会显示一个包含很长消息的AlertDialog。我一直在尝试不同的方法使它可以滚动,但我做不到。我试着在StackOverflow上阅读不同的问题,但它们对我没有用。这是我的AlertDialog代码。
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();
有人能向我详细解释一下如何使它可滚动吗?如有任何帮助或建议,将不胜感激!
发布于 2013-06-05 22:27:39
此解决方案取自this post。
为了使视图可滚动,必须将其嵌套在ScrollView容器中:
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView />
<Button />
</LinearLayout>
</ScrollView>
注意,ScrollView容器只能有一个子布局视图。例如,在没有TextView的ScrollView中放置LinearLayout和Button是不可能的。
发布于 2013-06-05 22:47:41
在这种情况下,您可以创建自己的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>
活动课
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();
发布于 2017-11-09 07:44:21
可滚动警报对话框
您可以使用默认方法:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title").setMessage(message);
AlertDialog alert = builder.create();
alert.show();
您可以注意到,消息TextView是内置在alert_dialog.xml
中的ScrollView容器。它是一个被使用的布局。
alert_dialog.xml
的定位
<some_path>/Android/sdk/platforms/android-<version>/data/res/layout/alert_dialog.xml
//e.g.
/Users/alex/Library/Android/sdk/platforms/android-30/data/res/layout/alert_dialog.xml
https://stackoverflow.com/questions/16955053
复制相似问题