首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android -如何使这个警报对话框滚动?

Android -如何使这个警报对话框滚动?
EN

Stack Overflow用户
提问于 2013-06-06 06:24:58
回答 4查看 64.7K关注 0票数 25

我是一个android的初学者,并制作了我的第一个android应用。当点击我的“关于”菜单项时,会显示一个包含很长消息的AlertDialog。我一直在尝试不同的方法使它可以滚动,但我做不到。我试着在StackOverflow上阅读不同的问题,但它们对我没有用。这是我的AlertDialog代码。

代码语言:javascript
运行
复制
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();

有人能向我详细解释一下如何使它可滚动吗?如有任何帮助或建议,将不胜感激!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-06-06 06:27:39

此解决方案取自this post

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

代码语言:javascript
运行
复制
<ScrollView>
    <LinearLayout android:orientation="vertical"
            android:scrollbars="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true">
        <TextView />
        <Button />
    </LinearLayout>
</ScrollView>

注意,ScrollView容器只能有一个子布局视图。例如,在没有TextView的ScrollView中放置LinearLayout和Button是不可能的。

票数 36
EN

Stack Overflow用户

发布于 2013-06-06 06:47:41

在这种情况下,您可以创建自己的layout.xml文件,其中包含滚动视图下的文本视图。并在“文本视图”中设置“TextMessage”,使用“警告”对话框充气此布局。

yourxmlfile.xml

代码语言:javascript
运行
复制
<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>

活动课

代码语言:javascript
运行
复制
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();
票数 13
EN

Stack Overflow用户

发布于 2017-11-09 15:44:21

可滚动警报对话框

您可以使用默认方法:

代码语言:javascript
运行
复制
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的定位

代码语言:javascript
运行
复制
<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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16955053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档