Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
552 views
in Technique[技术] by (71.8m points)

dialog - Adding a vertical scrollbar to an AlertDialog in Android?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...