【问题标题】:In Android, how can I set a ListView item's height and width?在 Android 中,如何设置 ListView 项的高度和宽度?
【发布时间】:2011-03-13 17:25:27
【问题描述】:

我想创建一个界面,它有一个或两个列表视图,最终跨越整个屏幕,有两行。如何更改它们的高度,使它们垂直占据屏幕的 50%?

如果是这样,我很想知道如何做到这一点,无论方向如何。

关于宽度;假设我有两个列表视图,每个视图都有一行。如上所述,它们跨越整个屏幕。下面的列表视图中有两个可点击的项目,每个项目占据屏幕空间的 50%,水平

这可能吗?如果有,怎么做?

【问题讨论】:

    标签: java android listview height width


    【解决方案1】:

    您好,下面的代码将使用 full 创建一个界面,该界面具有一个或两个列表视图,最终跨越整个屏幕,两行。 Java文件如下,

    public class DoubleListView extends Activity {
        ListView listView,listView2;
        String[] titles = {"List 1 title1","List 1 title2","List 1 title3","List 1 title4","List 1 title5","List 1 title6","List 1 title7","List 1 title8","List 1 title9"};
        String[] titles2 = {"List 2 title1","List 2 title2","List 2 title3","List 2 title4","List 2 title5","List 2 title6","List 2 title7","List 2 title8","List 2 title9"};
        WindowManager wm;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
            int height = wm.getDefaultDisplay().getHeight();
    
            listView = new ListView(this);
            listView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,height/2-15));
            listView.setAdapter(new CustomListAdapetr(this, titles));
    
            listView2 = new ListView(this);
            listView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,height/2));
            listView2.setAdapter(new CustomListAdapetr(this, titles2));
    
            ((LinearLayout)findViewById(R.id.mailLayout)).addView(listView);
            ((LinearLayout)findViewById(R.id.mailLayout)).addView(listView2);
    
        }
        public class CustomListAdapetr extends BaseAdapter{
    
            private Context mContext;
            private String[] list;
    
            public CustomListAdapetr(Context context, String[] titles) {
                mContext = context;
                list = titles;
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list.length;
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return list[position];
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                CustomView cv;
                if (convertView == null) {
                    cv = new CustomView(mContext,""+list[position]);
                    } 
                else {
                        cv = (CustomView) convertView;
    
                        cv.setTitle(list[position]);
    
                        }
                return cv;
            }
    
        }
    
        private class CustomView extends LinearLayout {
            public CustomView(Context context, String itemName) {
                super(context);
                this.setOrientation(HORIZONTAL);
                // Here we build the child views in code. They could also have
                // been specified in an XML file.
    
    
                mTitle = new TextView(context);
                mTitle.setText(itemName);
    
                mTitle.setTextSize(25);
    
                addView(mTitle, new LinearLayout.LayoutParams(200, LayoutParams.WRAP_CONTENT));
    
    
                }
            /**         * Convenience method to set the title of a SpeechView         */
            public void setTitle(String title) {
                mTitle.setText(title);
                }
            /**         * Convenience method to set the dialogue of a SpeechView         */
    
            private TextView mTitle;
    
            }
    
    }
    

    xml文件是,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/mailLayout">
    
    </LinearLayout>
    

    试试这个以获得你想要的界面。

    【讨论】:

    • 虽然我已经转移到其他项目,但我会相信你得到的五个支持。如果您尝试过此解决方案并且有效,请通过评论确认。谢谢!
    • 我的意思是其他人也是为了避免“它适用于我的机器/配置/版本的 Android”问题。 :) 感谢您的回答!
    猜你喜欢
    • 2014-08-07
    • 2018-09-14
    • 1970-01-01
    • 2017-02-23
    • 2015-12-14
    • 2013-02-06
    • 2015-04-24
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多