簡體   English   中英

如何將收藏夾 (CheckBox/ToggleButton/ImageView) 添加到我的 ListView?

[英]How can i add favourite (CheckBox/ToggleButton/ImageView) to my ListView?

請幫我。

如何將收藏夾 (CheckBox/ToggleButton/ImageView) 添加到我的 ListView? ...我以前從未添加過喜歡的按鈕,所以我想在 listView 中添加一個帶有 CheckBox 或 ToggleButton 或 ImageView 的按鈕。

希望大家能理解我的問題

我的代碼:

這是我的 ListAdapter

    static class ListAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        private Context ctx;

        public ListAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            ctx = context;
        }

        public int getCount() {
            return RecipeName.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;

            if(convertView == null){
                convertView = inflater.inflate(R.layout.row, null);
                holder = new ViewHolder();
                holder.txtRecipeName = (TextView) convertView.findViewById(R.id.txtRecipeName);


                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            holder.txtRecipeName.setText(RecipeName[position]);

            return convertView;
        }

        static class ViewHolder {
            TextView txtRecipeName;
        }
    }

這是我的行 xml

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="1"
    android:gravity="center_horizontal"
    android:background="@drawable/row">

    <TextView
        android:id="@+id/txtRecipeName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="@color/text"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_weight="0.04"
        android:typeface="normal"
        android:background="@drawable/button2"/>
</LinearLayout>

要添加圖像,您可以在.xml 文件中使用與 TextView 相同的 ImageView。

只需像這樣在“android:src="@drawable/beach”之后添加圖像的src。這里“@drawable/beach”是圖像的路徑。

`<圖像視圖

     android:id="@+id/photo_image_view" //for id

     android:layout_width="match_parent"

     android:layout_height="match_parent"

     android:scaleType="centerCrop" //this will display image in center

     android:src="@drawable/beach" /> //path of image`

對於切換按鈕..在.xml 文件中使用以下代碼

<開關

android:id="@+id/toggle_switch" //給 id 這樣可以在 java 代碼中使用它

android:layout_width="wrap_content" //設置寬度

android:layout_height="wrap_content" //設置高度

android:text="Toggle Button" // 要顯示的字符串

android:textAppearance="?android:textAppearanceSmall" />

外觀如何切換按鈕

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM