将垂直滚动条添加到警报对话框

时间:2012-08-05 08:42:33

标签: android

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

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

1 个答案:

答案 0 :(得分: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框大小时,它将显示滚动条。 希望这会对你有所帮助:)。