在Android中,我怎么可以设置一个ListView项目的高度和宽度? [英] In Android, how can I set a ListView item's height and width?

查看:136
本文介绍了在Android中,我怎么可以设置一个ListView项目的高度和宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个具有最终覆盖整个屏幕,两行一两个列表视图的接口。我怎样才能改变自己的高度,使他们采取了屏幕的50%,垂直?

I want to create an interface that has one or two listviews that ultimately span the entire screen with two rows. How can I change their height so that they take up 50% of the screen, vertically?

如果它是沿着相同的路线,我很想知道怎么不管做定位了这一点。

If it's along the same lines, I'd love to know how to do this regardless of orientation.

至于宽度;让我们说我有两个列表视图,各有一行。它们跨越整个屏幕,如上所述。下列表视图中有两份可点击的项目,每个占用屏幕空间的50%,水平的。

With regards to width; let us say I have two listviews, each with one row. They span the entire screen as described above. The lower listview has two clickable items within it that each take up 50% of the screen space, horizontally.

这可能吗?如果是这样,怎么样?

Is that possible? If so, how?

推荐答案

嗨以下code将使用全创建具有最终跨越两排整个屏幕一个或两个列表视图的接口。 Java的文件有:

Hi the following code will use full to create an interface that has one or two listviews that ultimately span the entire screen with two rows. The Java file is as follows,

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>

试试这个来获得您想要的界面。

Try this to get your desired interface.

这篇关于在Android中,我怎么可以设置一个ListView项目的高度和宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆