【问题标题】:How to put many objects in a scroll view entry in android?如何在android的滚动视图条目中放置许多对象?
【发布时间】:2017-02-23 21:26:22
【问题描述】:

我想创建一个数据库,在其中获取一些数据,例如名称、两个由切换按钮和清单表示的布尔值,并在滚动视图中列出所有内容,如图 1。我还想在滚动视图的同一条目中添加一个“删除”和“更新”按钮。

我看到你可以在ScrollView 中创建的垂直Linearlayour 中放置一个水平LinearLayout,这样我可以将文本和按钮放在同一行。但是当我这样做时,我无法为切换和复选框信息添加另一行,也无法为整个条目添加通用背景。

谁能告诉我应该如何解决这个问题?我试图在 xml 主布局中解决它只是为了得到一个想法然后将其转换为代码,但我无法让它工作。

【问题讨论】:

标签: android list object


【解决方案1】:

您只需要为每个列表项添加另一个 LinearLayout 作为容器:

- ScrollView
    - LinearLayout (vertical) -> List container
        - LinearLayout (vertical) -> Item container
            - LinearLayout (horizontal)
                ...some elements....
            - End of LinearLayout (horizontal)
            - LinearLayout (horizontal)
                ...more elements....
            - End of LinearLayout (horizontal)
        - End of LinearLayout (vertical) -> Item container
        ...More item containers...
    - End of LinearLayout (vertical) -> List container
- End of ScrollView

这不是您图像中的确切布局,但您应该按照此模式解决它。

尽管如此,正如其他人在他们的 cmets 中所说,这可以使用 ListView 或更好的 RecyclerView 以更有效的方式实现。

我现在不能给你一个真实的例子,如果你需要,我稍后会做。

【讨论】:

    【解决方案2】:

    我不确定我是作为评论还是作为答案做出回应。我已经按照@Alberto Méndez 的回答并更改为 cmets 所说的列表,我认为我得到了解决方案,我仍然需要努力使外观更好,但结构是我想要做的: 辅助列表.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="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background_gradient">
    
        <LinearLayout
            android:layout_width="255dp"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/main_name_list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Title_entry"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textStyle="bold" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
                    <CheckBox
                        android:id="@+id/checkbox_list"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Checkbox"
                        android:clickable="false" />
                    <Switch
                        android:id="@+id/switch_button_list"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textOff="Text off"
                        android:textOn="Text on"
                        android:clickable="false" />
    
                </LinearLayout>
    
            </LinearLayout>
    
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center">
    
            <Button
                android:layout_width="5dp"
                android:layout_height="30dp"
                android:id="@+id/update_entry_list"
                android:background="@android:drawable/ic_menu_edit"
                android:layout_weight="0.5"
                android:height="10dp" />
            <Button
                android:layout_width="5dp"
                android:layout_height="30dp"
                android:id="@+id/delete_entry_list"
                android:background="@android:drawable/ic_delete"
                android:layout_weight="0.5"
                android:height="10dp"/>
    
        </LinearLayout>
    
    </LinearLayout>
    
    </LinearLayout>
    

    主要活动.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/screen_padding"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/background_gradient">
    
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/title"
        android:id="@+id/manage_exercises_title"
        android:text="@string/title"/>
    
    <Space
        android:layout_width="match_parent"
        android:layout_height="@dimen/content_spacing" />
    
    <ListView
        android:id="@+id/list_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    
    </ListView>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="10dp"
        android:src="@android:drawable/ic_input_add" />
    </LinearLayout>
    

    用例子填充的Java代码:

    package com.example.nck.routine_tracker;
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CheckBox;
    import android.widget.ListView;
    import android.widget.Switch;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.util.Arrays;
    /**
     * Created by nck on 14/10/16.
     */
    public class MainJavaFile extends Activity{
    ListView list;
    String [] text = new String[20];
    boolean [] option1 = new boolean[20];;
    boolean [] option2 = new boolean[20];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity_xml);
        for (int i=0;i<20;i++){
            text[i]=""+ i + "º Entry";
        }
        list = (ListView) findViewById(R.id.list_1);
        list.setAdapter(new listAdapter(text,option1,option2, this.getBaseContext()));
        Toast.makeText(this, text[0],Toast.LENGTH_LONG).show();
    }
    }
    class listAdapter extends BaseAdapter {
        String[] text;
        boolean[] option1, option2;
        Context context;
        LayoutInflater inflater;
    
        public listAdapter() {
            text = null;
            option1 = null;
            option2 = null;
        }
    
        public listAdapter(String[] text, boolean[] option1, boolean[] option2, Context context) {
            this.text = text;
            this.option1 = option1;
            this.option2 = option2;
            this.context = context;
        }
    
        public int getCount() {
            // TODO Auto-generated method stub
            return text.length;
        }
    
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }
    
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row;
            row = inflater.inflate(R.layout.auxiliary_list, parent, false);
            TextView Text;
            CheckBox Option1;
            Switch Option2;
            Text = (TextView) row.findViewById(R.id.main_name_list);
            Option1 = (CheckBox) row.findViewById(R.id.checkbox_list);
            Option2 = (Switch) row.findViewById(R.id.switch_button_list);
            Text.setText(text[position]);
            Option1.setChecked(option1[position]);
            Option2.setEnabled(option2[position]);
            return (row);
        }
    }
    

    以及解决方案:

    【讨论】:

    • 很高兴听到你得到它
    猜你喜欢
    • 2011-07-08
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多