簡體   English   中英

Android - 如何使此警報對話框可滾動?

[英]Android - How do I make this alert dialog scrollable?

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

任何人都可以向我詳細解釋如何使其可滾動? 任何幫助或建議將不勝感激!

這個解決方案取自這篇文章

為了使視圖可滾動,它必須嵌套在 ScrollView 容器內:

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

請注意,一個 ScrollView 容器只能有一個子布局視圖。 例如,不可能在沒有 LinearLayout 的情況下將 TextView 和 Button 放置在 ScrollView 中。

在這種情況下,您可以創建自己的 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();

可滾動警報對話框

您可以只使用默認方法:

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

或者您可以以編程方式執行此操作。 如果您需要非標准對話框,這將特別有用。 (例如,在下面的示例中,除了我的消息之外,我還想添加一個 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM