【问题标题】:Android - How do I make this alert dialog scrollable?Android - 如何使此警报对话框可滚动?
【发布时间】:2013-06-06 06:24:58
【问题描述】:

我是一名 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();

谁能详细解释一下如何使它可滚动? 任何帮助或建议将不胜感激!

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

此解决方案取自this post

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

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

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

【讨论】:

  • 我猜你通常需要设置滚动视图的宽度/高度;就像这里问题中的示例一样stackoverflow.com/questions/17352987/…
  • 这是最简单的解决方案!谢谢 !像魅力一样工作;)
【解决方案2】:

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

你的xml文件.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();

【讨论】:

  • 在这里使用你的 xml TextView 的 id,你在`
  • 我必须解决这个问题。此行需要修改.. (TextView)view.findViewById(R.id.textmsg);
  • 没关系,经过一些更改后我得到了它的工作对不起我上面的评论我可以编辑它:(
  • @cutiko 再看答案,只有一个直接子视图LinearLayout
【解决方案3】:

可滚动的警报对话框

您可以只使用默认方法:

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

【讨论】:

    【解决方案4】:

    或者您可以通过编程方式进行。如果您想要一个非标准对话框,这将特别有用。 (例如,在下面的示例中,我想在我的消息之外添加一个 EditText 字段。)您也可以像往常一样添加按钮。

    final ScrollView myScroll = new ScrollView(this);
            final TextView myText = new TextView(this);
            myText.setText("Put really long text here.");
            myText.setPadding(30, 5, 30, 0); //or whatever you want
            final LinearLayout myView = new LinearLayout(this);
            myView.setOrientation(LinearLayout.VERTICAL);
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_TEXT );
            myView.addView(myText);
            myView.addView(input);
            myScroll.addView(myView);
            builder.setView(myScroll);
    

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多