向 Android 中的 AlertDialog 添加垂直滚动条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1564867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a vertical scrollbar to an AlertDialog in Android?
提问by Hubert
I would like to add a vertical scrollbar to an AlertDialog because my text is too long to display on 1 screen:
我想向 AlertDialog 添加一个垂直滚动条,因为我的文本太长而无法在 1 个屏幕上显示:
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:
这是我正在使用的 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>
I call the AlertsDialog with :
我调用 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();
}
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>
回答by John Leehey
In order for a view to scrollable, it must be nested inside of a ScrollView
container:
为了使视图可滚动,它必须嵌套在ScrollView
容器内:
<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
.
请注意,一个ScrollView
容器只能有一个子布局视图。例如,不能将 aTextView
和Button
a放在ScrollView
不带 的情况下LinearLayout
。
回答by Melbourne Lopes
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());
回答by Hanisha
Using this it works :
使用它可以工作:
.setScrollable(true)
Use this in your AlertDialogBox or MaterialStyledDialog.
在您的 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();
}
回答by BitByteDog
On Android Q the scrollbars can be made always visible.
在 Android Q 上,滚动条可以始终可见。
After creating and showing the dialog, find the TextView holding the message and modify it like this. (BTW the call to setMaxLines(5)
is just to limit the number of visible lines to demonstrate the scrolling. Set it as you will, or remove it):
创建并显示对话框后,找到保存消息的 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));
}
Two drawables are needed.
需要两个drawable。
For app/src/main/res/drawable/scrollbar_thumb_vertical.xml
对于 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>
And for app/src/main/res/drawable/scrollbar_thumb_vertical.xml
而对于 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>