Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在行之间创建具有不同背景的警报对话框列表_Java_Android_Android Alertdialog - Fatal编程技术网

Java 如何在行之间创建具有不同背景的警报对话框列表

Java 如何在行之间创建具有不同背景的警报对话框列表,java,android,android-alertdialog,Java,Android,Android Alertdialog,如何在警报对话框中设置背景奇偶行? 您必须使用builder.setView实现自定义视图 然后,如果使用ListView,只需使用以下内容: @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = ...; } convertView.setBackgroundCo

如何在警报对话框中设置背景奇偶行?


您必须使用builder.setView实现自定义视图

然后,如果使用ListView,只需使用以下内容:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ...;
    }
    convertView.setBackgroundColor(position % 2 ? Color.GREY : Color.WHITE);
    ...
}

cf

您可以设置自己的适配器,而不是设置项

AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity.this);
    builder.setTitle("Pilih Tipe User");
    // Create an ArrayAdapter from List
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, new String[]{"test1", "test2", "test3"}){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            // Get the current item from ListView
            View view = super.getView(position,convertView,parent);
            if(position %2 == 1)
            {
                // Set a background color for ListView regular row/item
                view.setBackgroundColor(Color.parseColor("#FFB6B546"));
            }
            else
            {
                // Set the background color for alternate row/item
                view.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
            }
            return view;
        }
    };
    builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int position) {
        }
    }).show();
在getView方法中

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.file_list_row, parent, false);

        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.layout_id);

    //set the background color of alternative row
            if (position % 2 == 0) {
                linearLayout.setBackgroundColor(Color.parseColor("#BDBDBD"));

            } else {
                linearLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));
            }
return view;
}
在原始xml文件中

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

    <LinearLayout
        android:id="@+id/layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

/* Your Views */

    </LinearLayout>
</LinearLayout>

您必须使用from recyclerView和自定义适配器,如下所示:

private void selectCategoryDialog() {
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
    dialog.setContentView(R.layout.chips_dialog_layout);
    dialog.setCancelable(true);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    RecyclerView recyclerView = dialog.findViewById(R.id.recyclerView);
    TextView dialogTitle = dialog.findViewById(R.id.dialog_title);

    dialogTitle.setText(getResources().getString(R.string.category));

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    AdapterCategory _adapter = new AdapterCategory(this, placeCategories);
    recyclerView.setAdapter(_adapter);
    _adapter.setOnItemClickListener((view, obj, position) -> {
        addCategoryChip(obj);
        dialog.hide();
    });

    HelperFunctions.setFont(dialog
            .findViewById(android.R.id.content), this, Constants.iranSansFont);

    dialog.show();
    dialog.getWindow().setAttributes(lp);
}
适配器:

对话框的自定义布局:


我希望能帮助您

创建Listview并为其设置适配器。使用builder.setItems无法更改行项目的背景色。我在位置上遇到问题,当我滚动项目时,第一个和第二个项目的背景色相同,但谢谢broWelcome。很乐意帮忙。
private void selectCategoryDialog() {
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
    dialog.setContentView(R.layout.chips_dialog_layout);
    dialog.setCancelable(true);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    RecyclerView recyclerView = dialog.findViewById(R.id.recyclerView);
    TextView dialogTitle = dialog.findViewById(R.id.dialog_title);

    dialogTitle.setText(getResources().getString(R.string.category));

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    AdapterCategory _adapter = new AdapterCategory(this, placeCategories);
    recyclerView.setAdapter(_adapter);
    _adapter.setOnItemClickListener((view, obj, position) -> {
        addCategoryChip(obj);
        dialog.hide();
    });

    HelperFunctions.setFont(dialog
            .findViewById(android.R.id.content), this, Constants.iranSansFont);

    dialog.show();
    dialog.getWindow().setAttributes(lp);
}
public class AdapterCategory extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<PlaceCategory> items = new ArrayList<>();


private OnItemClickListener mOnItemClickListener;
private Typeface mTypeface;

public interface OnItemClickListener {
    void onItemClick(View view, PlaceCategory obj, int position);
}

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mOnItemClickListener = mItemClickListener;
}

public AdapterCategory(Context context, List<PlaceCategory> items) {
    this.items = items;
    this.mTypeface = HelperFunctions.getTypeface(context);
}

public class OriginalViewHolder extends RecyclerView.ViewHolder {
    public ImageView image;
    public TextView title;
    public View lyt_parent;

    public OriginalViewHolder(View v) {
        super(v);
        image = v.findViewById(R.id.image);
        title = v.findViewById(R.id.type_title);
        lyt_parent = v.findViewById(R.id.lyt_parent);
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder vh;
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chips_item, parent, false);
    HelperFunctions.setFontForAdapter((ViewGroup) v, mTypeface);
    vh = new OriginalViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof OriginalViewHolder) {
        OriginalViewHolder view = (OriginalViewHolder) holder;

        PlaceCategory p = items.get(position);
        view.title.setText(p.toString());
        view.lyt_parent.setOnClickListener(view1 -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(view1, items.get(position), position);
            }
        });

        view.lyt_parent.setBackgroundColor(position % 2 == 0 ? Color.GRAY : Color.WHITE);
    }
}

@Override
public int getItemCount() {
    return items.size();
}
    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="@dimen/spacing_large"
        android:text="Contact List"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/spacing_middle"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>