Adding a vertical scrollbar to an AlertDialog in A

2019-01-08 15:47发布

I would like to add a vertical scrollbar to an AlertDialog because my text is too long to display on 1 screen:

I have tried to use :

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

but the scrollbars don't even display ?

Here's the xml layout file I'm using:

<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>

I call the AlertsDialog with :

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();
 }

I found the answer now=> IT WORKS NOW WITH THIS :

<?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>

3条回答
放荡不羁爱自由
2楼-- · 2019-01-08 16:07

In order for a view to scrollable, it must be nested inside of a ScrollView container:

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

Note that a ScrollView container can only have one child layout view. It is not possible, for example, to place a TextView and Button in a ScrollView without the LinearLayout.

查看更多
男人必须洒脱
3楼-- · 2019-01-08 16:12
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());
查看更多
ら.Afraid
4楼-- · 2019-01-08 16:15

Using this it works :

 .setScrollable(true)

Use this in your AlertDialogBox or 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();

}
查看更多
登录 后发表回答