机器人列表视图填充高度到内容 [英] Android List view fill the height to the contents

查看:120
本文介绍了机器人列表视图填充高度到内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML定义的列表视图,现在我设置的内容视图的setContentView(R.layout.topic_layout); ,我有5个项目在里面,目前其填充列表视图中,只有一半的高度,但我希望它完全填充的高度,使得我没有在底部的任何空间。

我已经寻找,但无法找到任何解决办法,请大家帮我达致这:

和还我设置的适配器是这样的:

 适配器=新MyAdapter(本);
        如果(适配器!= NULL){
        setListAdapter(适配器);

        }
 

解决方案

如果你有一个项目一个固定的数,并希望他们一路延伸到屏幕的结束,ListView控件是不适合你的最佳选择。使用的LinearLayout这会占用所有的空间,并添加所有的项目吧。这是假设你想要的物品,每次占用所有的空间。

使用的LinearLayout,你可以发$ P $垫的物品出均匀没有做自己的任何计算。

 的LinearLayout的LinearLayout =新的LinearLayout(getSupportActivity());
linearLayout.setOrientation(android.widget.LinearLayout.VERTICAL);

RelativeLayout.LayoutParams PARAMS =新的LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

的for(int i = 0;我小于5;我++){
    查看individualView =新景(getSupportActivity());
    //在这里创建自定义视图,并把它添加到线性布局
    //保留高度为0,将LinearLayout中计算出的高度正确。
    PARAMS =新的LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,0);
    additionalOption.setLayoutParams(PARAMS);

    //作为,我们将它添加到线性布局,它们都将有一个权重为1,这将使他们小号$ P $垫出均匀。
    linearLayout.addView(additionalOption);
}
mainView.addView(的LinearLayout);
 

编辑:如果你已经实现了它与ListView和麻烦去改变它,你可以做以下

确认列表视图的宽度和高度都设置在XML来match_parent。然后在其中创建自定义视图适配器的getView(),请执行下列操作

  //获取的ListView的高度
INT totalHeight = listView.getHeight();
INT的rowHeight = totalHeight / getCount将(); //除以项目数。

//创建与上面计算的高度自定义视图。
 

注意有关totalHeight为0。如果您创建的onCreate(ListView控件),并设置的onCreate适配器(),以及,在ListView将最有可能不会有尚未计算的宽度或高度。尝试(),而不是设置适配器onResume。由这点,在ListView的尺寸将被计算并进行布置在屏幕上。

希望这有助于。

i have a list view defined in the xml, now i am setting the content view setContentView(R.layout.topic_layout); , i have 5 items in it,currently its filling only half the height of the list view but i want it to completely fill the height so that i dont have any space at the bottom.

i have searched for it but couldnt find any solution, please help me to acheive this :

and also i am setting the adapter like this:

adapter = new MyAdapter(this);
        if (adapter != null) {
        setListAdapter(adapter);

        }

解决方案

If you have a fixed number of items and want them to stretch all the way to the end of the screen, ListView is not the best choice for you. Use a LinearLayout which takes up all the space and add all the items to it. This is assuming you want the items to take up all the space every time.

Using LinearLayout, you can spread the items out evenly without doing any calculations yourself.

LinearLayout linearLayout = new LinearLayout(getSupportActivity());
linearLayout.setOrientation(android.widget.LinearLayout.VERTICAL);

RelativeLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);

for (int i = 0; i < 5; i++) {
    View individualView = new View(getSupportActivity());
    // Create your custom view here and add it to the linear layout
    // Leave the height as 0, LinearLayout will calculate the height properly.
    params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
    additionalOption.setLayoutParams(params);

    // As a we are adding it to the linear layout, they will all have a weight of 1, which will make them spread out evenly.
    linearLayout.addView(additionalOption);
}
mainView.addView(linearLayout);

EDIT: If you have already implemented it with ListView and is troublesome to change it, you can do the following.

Make sure the list view width and height are set to match_parent in the xml. Then in getView() of the adapter where you create your custom view, do the following

// Get the height of the ListView
int totalHeight = listView.getHeight();
int rowHeight = totalHeight/getCount(); // Divide by number of items.

// Create custom view with the height calculated above.

Be careful about the totalHeight being 0. If you create the ListView in onCreate() and set the adapter in onCreate() as well, the ListView will most likely not have the width or height calculated yet. Try setting the adapter in onResume() instead. By this point, the dimensions of the ListView would have been calculated and laid out on the screen.

Hope this helps.

这篇关于机器人列表视图填充高度到内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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