开发者

Problem when using more elements in the ListView

I have a ListView and a button in my view. Button is placed just below the listview. My listview contains 10 items. When I am running the application. I can't see the button inside the view. Because of 10 items i have to scroll the listview to see all items. If i use 4 items, I can see the button. Why it is happened?开发者_Go百科 Please help me. Thank you..


I think you want button to be displayed always on the screen whatever maybe the size of the screen/list. Just make your layouts to be AbsoluteLayouts as below

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

<Button android:id="@+id/button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Pixels" 
android:layout_y="50px" 
android:layout_x="80px"
android:focusable="true">
</Button>

   <ListView android:id="@+id/ListView01"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    </ListView>

</AbsoluteLayout>

& in you UI thread use bringToFront on that button as below

        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);

            Button b= (Button) findViewById(R.id.button);
            b.setText("Button");
                b.bringToFront();
....
    }

and you are done!


Sounds like you might not use layout correctly. Are you using a linear layout in the view? If so you might want to look at relative layout. Align your button to parentBottom and make you list align to parentTop and to above your button. That should make the list scroll and not expand.


check the width of your views, make sure it is set to wrap_contnet


You can modify your xml like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <ListView android:layout_width="match_parent" 
       android:id="@+id/listVie" 
       android:layout_weight="0.80" 
       android:layout_height="0dp">
    </ListView>

    <Button  
       android:layout_width="fill_parent"
       android:layout_weight="0.20" 
       android:layout_height="0dp"/>
</LinearLayout>

Look at the weight property:

android:weightSum="1"

android:layout_weight="0.80"
android:layout_weight="0.20"

They are making it :)


You shold use

android:weightSum="10"

android:layout_weight="8"
android:layout_weight="2"


I have a ListView and a button in my view. Button is placed just below the listview. My listview contains 10 items. When I am running the application. I can't see the button inside the view.

You can use relative layout something like this to achieve

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000">

<ListView
    android:id="@+id/frg_product_inq_productSp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/btn_add"
    android:layout_alignParentTop="true"
    android:divider="#ffffff"
    android:dividerHeight="0.5dp"
    android:gravity="center" />
<Button
    android:id="@+id/btn_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:text="ADD"
    android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

And i.e your output will look like below for all devices

Problem when using more elements in the ListView

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜