Adding a vertical scrollbar to an AlertDialog in Android?(向Android中的AlertDialog添加垂直滚动条?)
问题描述
我想为 AlertDialog 添加一个垂直滚动条,因为我的文本太长而无法在 1 个屏幕上显示:
I would like to add a vertical scrollbar to an AlertDialog because my text is too long to display on 1 screen:
我试过用:
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
但滚动条甚至不显示?
这是我正在使用的 xml 布局文件:
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>
我用 :
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>
推荐答案
为了让视图可滚动,它必须嵌套在 ScrollView
容器内:
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>
请注意,ScrollView
容器只能有一个子布局视图.例如,不能在没有 LinearLayout
的情况下将 TextView
和 Button
放置在 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
.
这篇关于向Android中的AlertDialog添加垂直滚动条?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向Android中的AlertDialog添加垂直滚动条?


- Android - 拆分 Drawable 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01