Example where the listview Button is always placed at the bottom

Source: Internet
Author: User

Android uses RelativeLayout to implement the bottom layout, and sets android: layout_alignParentBottom = "true" to easily implement the bottom layout. However, simple attribute settings for complex la s cannot achieve this effect. For example, the top, center, and bottom la s may be caused by the middle layer (center) too much data will not be displayed completely or the bottom layer will be squeezed down. To solve this problem, when using RelativeLayout layout, in addition to setting android: layout_alignParentBottom = "true", you also need to set properties for the middle layer: android: layout_above = "@ id/bottom"
Android: layout_below = "@ id/top ". This setting ensures that the center layer is in the middle, and the scroll bar can be displayed automatically.

The following example shows how to implement the bottom layout of a three-layer layout. 1, 2.
 
Figure-1 bottom layout of Layer 3
 
Figure 2 bottom button displayed during Input
The project only implements the main data filling and layout, so it is just a simple file loading. The following is the source code:
BottomTestActivity. java

Copy codeThe Code is as follows:
Package com. BottomTest. main;

Import java. util. ArrayList;
Import java. util. HashMap;

Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ListView;
Import android. widget. SimpleAdapter;

Publicclass BottomTestActivityextends Activity {

/** Called when the activity is first created .*/
@ Override
Publicvoid onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
ListView list = (ListView) findViewById (R. id. friends );
// List of arrays for storing data
ArrayList <HashMap <String, Object> listData = new ArrayList <HashMap <String, Object> ();
String [] name = {"William", "Charles", "Linng", "Json", "Bob", "Carli "};
String [] id = {"12", "16", "33", "21", "34", "22 "};
For (int I = 0; I <6; I ++ ){
HashMap <String, Object> map = new HashMap <String, Object> ();
Map. put ("friend_image", R. drawable. icon );
Map. put ("friend_username", name [I]);
Map. put ("friend_id", id [I]);
ListData. add (map );
}
// Adapter
SimpleAdapter listItemAdapter = new SimpleAdapter (this,
ListData,
R. layout. item,
New String [] {"friend_image", "friend_username", "friend_id "},
Newint [] {R. id. friend_image, R. id. friend_username, R. id. friend_id });
List. setAdapter (listItemAdapter );
}
}


Main layout File
Main. xml

Copy codeThe Code is as follows:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical">
<RelativeLayoutandroid: id = "@ + id/bottom"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content">
<LinearLayoutandroid: id = "@ + id/top"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<EditTextandroid: id = "@ + id/view_user_input"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "6dip"
Android: layout_marginLeft = "12dip"
Android: singleLine = "true"
Android: numeric = "integer"
Android: imeOptions = "actionDone"
Android: hint = "enter user ID"
Android: layout_weight = "1"/>
<Buttonandroid: id = "@ + id/view_user"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "4dip"
Android: layout_weight = "3"
Android: text = "View"/>
</LinearLayout>
<LinearLayoutandroid: id = "@ + id/center"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical"
Android: layout_above = "@ id/bottom"
Android: layout_below = "@ id/top">
<TextViewandroid: id = "@ + id/my_friends_list"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "friend list"
Android: paddingTop = "6dip"
Android: paddingLeft = "2dip"
Android: layout_marginLeft = "10dip"/>
<ListViewandroid: id = "@ + id/friends"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginBottom = "6dip"/>
</LinearLayout>
<LinearLayoutandroid: id = "@ + id/bottom"
Android: background = "@ drawable/bg"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: layout_alignParentBottom = "true">
<Buttonandroid: id = "@ + id/refresh"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "2dip"
Android: text = "Refresh user list"
Android: layout_weight = "1"/>
<Buttonandroid: id = "@ + id/back"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "2dip"
Android: text = "return"
Android: layout_weight = "1"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>


Layout file of listview item content
Item. xml

Copy codeThe Code is as follows:


<? Xmlversion = "1.0" encoding = "UTF-8"?>
<RelativeLayoutxmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/RelativeLayout"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: paddingBottom = "4dip"
Android: paddingRight = "12dip">
<ImageViewandroid: id = "@ + id/friend_image"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: paddingTop = "6dip"
Android: paddingLeft = "2dip"
Android: layout_centerVertical = "true"
Android: layout_alignParentLeft = "true"/>
<TextViewandroid: id = "@ + id/friend_username"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textSize = "18dip"
Android: textColor = "# ccc"
Android: paddingTop = "6dip"
Android: paddingRight = "2dip"
Android: layout_toRightOf = "@ id/friend_image"/>
<TextViewandroid: id = "@ + id/friend_id"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_below = "@ + id/friend_username"
Android: layout_marginRight = "36dip"
Android: paddingRight = "2dip"
Android: layout_toRightOf = "@ id/friend_image"
Android: textColor = "# fff"
Android: maxLines = "2"/>
</RelativeLayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.