滚动视图在警报对话框中不起作用

时间:2018-08-06 08:45:27

标签: android scroll alertdialog

以某种方式无法使用滚动视图。显示的消息警报对话框确实很大,因此我需要实现垂直滚动条。我试图从上一个问题中获取数据,但仍不能解决我的问题,请帮忙。

我需要在按钮单击事件上显示警报对话框。

 benefits.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
                ad.setCancelable(false); // This blocks the 'BACK' button
                ad.setMessage(getString(R.string.benefits));
                ad.setTitle("Benefits");
                ad.setButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

                ad.show();
                TextView textView = (TextView) ad.findViewById(android.R.id.message);

                textView.setScroller(new Scroller(Panchgavya.this));
                textView.setVerticalScrollBarEnabled(true);
                textView.setMovementMethod(new ScrollingMovementMethod());


            }
        });

我的XML文件代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Design.Panchgavya"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/Green"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Panchgavya"
            android:textColor="@color/white"/>

      </android.support.v7.widget.Toolbar>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:orientation="vertical">
        <TextView
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:ellipsize="end"
            android:text="@string/panchgavya"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="vertical">
        <TextView
            android:id="@+id/panchgavya_tv_cow_dung"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Cow Dung"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textStyle="bold"
            android:textColor="@color/black"/>
        <TextView
            android:id="@+id/panchgavya_tv_cow_urine"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Cow Urine"
            android:textStyle="bold"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/black"/>
        <TextView
            android:id="@+id/panchgavya_tv_cow_milk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Cow Milk"
            android:textStyle="bold"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@color/black"/>
        <TextView
            android:id="@+id/panchgavya_tv_ghee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:text="Ghee"
            android:textStyle="bold"
            android:textColor="@color/black"/>
        <TextView
            android:id="@+id/panchgavya_tv_dahi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:text="Dahi"
            android:textStyle="bold"
            android:textColor="@color/black"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true">
    <Button
        android:id="@+id/panchgavya_btn_benefits"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Benefits"
        android:background="@color/Green"
        android:textColor="@color/white"
        />
    </LinearLayout>


</LinearLayout>

4 个答案:

答案 0 :(得分:1)

您可以为对话框创建自定义布局,并轻松设置任何属性。

根据您的要求,为所需的对话框创建布局。将android:scrollbars = "vertical"放在布局内的textView中。还有textview.setMovementMethod(new ScrollingMovementMethod());

您可以通过以下方法在您的网站上设置自定义布局。

 public void showDialog(Activity activity, String msg){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog);

        TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
        text.setText(msg);

        Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }

答案 1 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


        <Textview
            android:id="@+id/txtDescription"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/margin_16dp"
            android:paddingRight="@dimen/margin_16dp"
            android:paddingBottom="@dimen/margin_16dp"
            android:paddingTop="@dimen/margin_10dp"
            android:text="@string/dummy_text_"
            android:textSize="@dimen/font_14dp"
            android:textColor="@color/colorPrimary"
            android:layout_below="@+id/imgClose"/>





    <ImageView
        android:id="@+id/imgClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close"
        android:layout_alignParentRight="true"
        android:layout_marginRight="@dimen/margin_10dp"
        android:layout_marginTop="@dimen/margin_10dp" />

   </RelativeLayout>

</ScrollView>

// in your java file put below code 
 private void showPopup() {
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_terms_services, 
null);
    dialogBuilder.setView(dialogView);

    TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
    ImageView imgClose = dialogView.findViewById(R.id.imgClose);
    txtDescription.setText(message);


    final AlertDialog b = dialogBuilder.create();
    b.show();
    imgClose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            b.dismiss();
        }
    });

}

//然后单击按钮后调用showPopup()方法。希望它对您有用

答案 2 :(得分:0)

扩展DialogFragment的类可以根据您的要求包含UI元素。

public class OrderDetailFragment extends DialogFragment{

    @Override
    public void onStart() {
        super.onStart();
        Dialog d = getDialog();
        if (d!=null){
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.WRAP_CONTENT;
            d.getWindow().setLayout(width, height);
        }
    }

   public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
        OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(DATA, dataList);
        orderDetailFragment.setArguments(bundle);
        return orderDetailFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root_view =  inflater.inflate(R.layout.fragment_order_detail, container, false);
        GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable

        //population of data from mDataList  

        return root_view;
    }

    void closeDialog(){
        this.dismiss();
    }

}

fragment_order_detail具有我的需求特定元素(ScrollView / LinearLayouts / Buttons等)

GeneralListDataPojo是用于保存可在组件之间传输数据的可打包数据实现的类

现在,您可以像这样从DialogFragment调用此Fragment。 (我从Fragment进行调用。如果您使用的是FragmentManager,请相应地更改Activity检索内容)

OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");

答案 3 :(得分:0)

使用滚动视图在自己的自定义布局中创建自定义对话框布局

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Dialog dialog= new Dialog(getApplicationContext());
        dialog.setContentView(R.layout.activity_dialog);
        Button click= dialog.findViewById(R.id.click);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
});