0

我正在应用程序中制作警报对话框。由于警报对话框中存在更多文本,我想在警报对话框中放置垂直滚动条。我尝试了很多选项,但没有任何效果。请告诉我如何解决这个问题问题。这是代码:

AlertDialog.Builder HelpOnButtonDialog ;
        HelpOnButtonDialog = new AlertDialog.Builder(this);
        TextView HelpOnButtonView = new TextView(this);
        HelpOnButtonView.setSingleLine(false);
        HelpOnButtonView.setTextColor(getResources().getColor(R.color.dark_green));
        HelpOnButtonView.setText("hello");  
        Button HelpOnButton = new Button(this);
        HelpOnButton.setHeight(20);
        HelpOnButton.setWidth(20);
        HelpOnButton.setText("Ok");
        HelpOnButton.setOnClickListener(HelpOnButtonClickListener);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(1); 
        linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
        linearLayout.addView(HelpOnButtonView);
        linearLayout.addView(HelpOnButton);

        ScrollView sv = new ScrollView(this);
        sv.pageScroll(0);
        sv.setBackgroundColor(0);
        sv.setScrollbarFadingEnabled(true);
        sv.setVerticalFadingEdgeEnabled(false);
        sv.addView(linearLayout);
        alertHelp = HelpOnButtonDialog.create();
        alertHelp.setView(sv);
        alertHelp.show();
4

1 回答 1

3

创建一个自定义对话框,您可以根据需要对其进行自定义。在对话框布局xml文件中

用可滚动的方式围绕您的 RelativeLayout 或 LinearLayout,如下面的代码

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:orientation="vertical"
    android:layout_weight="1">

    <!--Your other text views, buttons... etc surrounded by RelativeLayout
or LinearLayout goes here-->

</ScrollView>

当内容超过 dlalog 框大小时,它将显示滚动条。希望对你有帮助 :)

于 2013-01-03T11:13:19.230 回答