【问题标题】:Adding a vertical scrollbar to an AlertDialog in Android?向Android中的AlertDialog添加垂直滚动条?
【发布时间】:2010-12-06 14:20:30
【问题描述】:

我想向 AlertDialog 添加一个垂直滚动条,因为我的文本太长而无法在 1 个屏幕上显示:

我试过用:

android:scrollbars="vertical" 
android:scrollbarAlwaysDrawVerticalTrack="true"

但滚动条甚至不显示?

这是我正在使用的 xml 布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:id="@+id/instructions_view" >
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A LONG TEXT 1"/>

    <TextView 
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A LONG TEXT 2"/>

</LinearLayout>

我用 :

调用 AlertsDialog
public void onClick(View v) {
  switch(v.getId()){

    case R.id.Button_Instructions: 
     InstructionsDialog();
    break;

    case R.id.Button_Exit: 
     ExitDialog();
    break;
    }
 }

public void InstructionsDialog(){

  AlertDialog.Builder ad = new AlertDialog.Builder(this);
  ad.setIcon(R.drawable.icon);
  ad.setTitle("Instructions ...");
  ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));

  ad.setPositiveButton("OK", 
    new android.content.DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int arg1) {
      // OK, go back to Main menu
     }
    }
   );

   ad.setOnCancelListener(new DialogInterface.OnCancelListener(){
    public void onCancel(DialogInterface dialog) {
     // OK, go back to Main menu   
    }}
   );

  ad.show();
 }

我现在找到了答案 => 现在可以使用了:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:id="@+id/instructions_view" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A LONG TEXT 1"/>

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A LONG TEXT 2"/>

    </LinearLayout>
</ScrollView>

【问题讨论】:

  • FrameLayout 中的 ScrollView 是否可以正常工作?我应该如何更改 xml 文件来做到这一点?
  • 它的工作原理是在整个 周围添加一个 !太好了。
  • 我在帖子末尾发布了答案。干杯,H。
  • 您好,请将您的答案移至答案框并将其标记为答案。

标签: android dialog scrollbar


【解决方案1】:

此处找到的所有代码均已弃用。

您现在应该使用(在对话框的布局文件中)

嵌套滚动视图

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

此外,不再建议单独使用AlertDialog。您必须通过扩展 DialogFragment 来实现自己的 DialogFragment。

见:https://developer.android.com/guide/topics/ui/dialogs

然后你就可以调用你的对话片段了:

MyDialogFragment dialogFragment = MyDialogFragment.newInstance();
dialogFragment.show(fragment.getFragmentManager(), MyDialogFragment.TAG);

【讨论】:

    【解决方案2】:

    在 Android Q 上,滚动条可以始终可见。

    创建并显示对话框后,找到保存消息的 TextView 并像这样修改它。 (顺便说一句,对setMaxLines(5) 的调用只是为了限制可见行数以演示滚动。随意设置或删除它):

        TextView textView = (TextView) dialog.findViewById(android.R.id.message);
        textView.setMaxLines(5);
        textView.setVerticalScrollBarEnabled(true);
        textView.setMovementMethod(new ScrollingMovementMethod());
        textView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            textView.setScrollbarFadingEnabled(false);
            textView.setVerticalScrollbarTrackDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_track_vertical));
            textView.setVerticalScrollbarThumbDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_thumb_vertical));
        }
    

    需要两个可绘制对象。

    对于app/src/main/res/drawable/scrollbar_thumb_vertical.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    
        <gradient
            android:angle="0"
            android:endColor="#D8D8D8"
            android:startColor="#F7F7F7" />
    
        <corners android:radius="6dp" />
    
    </shape>
    

    对于 app/src/main/res/drawable/scrollbar_thumb_vertical.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
            android:angle="0"
            android:endColor="#757575"
            android:startColor="#A0A0A0" />
    
        <corners android:radius="6dp" />
    </shape>
    

    【讨论】:

      【解决方案3】:

      使用它可以工作:

       .setScrollable(true)
      

      在您的 AlertDialogBox 或 MaterialStyledDialog 中使用它。

       private void gettermsandconditions() {
          final MaterialStyledDialog dialogHeader_1 =
                  new MaterialStyledDialog.Builder(this)
                          .setTitle("Terms and Conditions !")
                         // .setDescription("What can we improve? Your feedback is always welcome.")
                          .setDescription(R.string.Terms_and_condition)
      
                          .setIcon(R.drawable.bill_icon)
                          .setStyle(Style.HEADER_WITH_ICON)
                          .setHeaderColor(R.color.colorPrimary)
                          .withDarkerOverlay(true)
                          .setScrollable(true)
                          .onPositive(new MaterialDialog.SingleButtonCallback() {
                              @Override
                              public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                //  startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName())));
                              }
                          })
                          .setNegativeText("Ok")
                          .build();
          //.setStyle(Style.HEADER_WITH_TITLE)
          dialogHeader_1.show();
      
      }
      

      【讨论】:

      • 一个很好的解决方案,但请考虑注明来源。 MaterialStyledDialog 是一个自定义的第三方类,不属于 Android Material Components 目录,所以有些人可能会感到困惑。
      【解决方案4】:
      AlertDialog dialog = new AlertDialog.Builder(this)
                      .setTitle("YOUR_TITLE")
                      .setMessage("YOUR_MSG")
                      .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();
                          }
                      })
                      .setIcon(android.R.drawable.ic_dialog_info)
                      .show();
              TextView textView = (TextView) dialog.findViewById(android.R.id.message);
              textView.setMaxLines(5);
              textView.setScroller(new Scroller(this));
              textView.setVerticalScrollBarEnabled(true);
              textView.setMovementMethod(new ScrollingMovementMethod());
      

      【讨论】:

      • 我必须删除这一行才能使滚动工作。 textView.setMaxLines(5);
      • 对textView.setMaxLines(5); 没有任何意义,并且您无需编辑 textview 属性即可使滚动工作,它默认工作。另一个问题 - 如何让滚动条可见?滚动有效,但您看不到滚动条
      • 这真是一个甜蜜的方法!
      • 谢谢。像魅力一样工作。就像我需要的一样。
      • 顺便说一句,它不适用于 AndroidX AlertDialog,只有框架之一(android.app.AlertDialog 而不是 androidx.appcompat.app.AlertDialog)。使用 AndroidX 时,我遇到了奇怪的行为,向下滚动卡住并且无法向上滚动。
      【解决方案5】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        相关资源
        最近更新 更多