Ответ 1
Вы можете использовать ListView # addFooterView(), чтобы добавить View
в нижней части ListView
.
Я хочу показать кнопку в конце списка Android-списка. Как я могу это достичь?
Я не хочу привязывать его к дню активности, используя alignparentbottom="true"
. Использование layout_below
тоже не работает для меня.
Мой текущий XML:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px" />
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:background="#676767"
android:orientation="vertical">
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</RelativeLayout>
</RelativeLayout>
Вы можете использовать ListView # addFooterView(), чтобы добавить View
в нижней части ListView
.
Я делаю это так, как эта фиксированная кнопка в нижней части экрана
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/btn_New" >
</ListView>
<Button
android:id="@+id/btn_New"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:text="@string/New"
android:width="170dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>
если ur использует linearLayout, тогда присвойте android: layout_weight = "1" в listview и не назначьте вес для кнопки, которую он работает.
Вы можете сделать что-то вроде этого:
final Button btnAddMore = new Button(this);
btnAddMore.setText(R.string.art_btn_moreIssues);
exArticlesList = (ExpandableListView) this.findViewById(R.id.art_list_exlist);
exArticlesList.addFooterView(btnAddMore);
1 Если вы хотите добавить кнопку в качестве последнего элемента в виде списка
Вы должны создать собственный ListAdapter для своего ListView, который создаст представление с помощью кнопки в методе getView. Вы должны решить, как вернуть свой пользовательский вид для последнего элемента, вы можете его жестко закодировать (число возвращаемых элементов +1 в методе getCount и возвращать пользовательский вид в getView при подсчете элемента position > ), или вы можете добавить элемент в структуру, в которой будете беря данные из (Array, Cursor и т.д.) и проверяя, имеет ли поле элемента определенное значение
2 Если вы хотите добавить элемент ниже списка списка
Вы должны использовать атрибут android: layout_width и создавать ListView и "пустой" TextView (вы должны использовать его, чтобы показать пользователям, что этот список пуст, и просмотр рендеринга завершен) layout_weight больше, чем кнопки layout_weight
Проверьте, как это делается в Transdroids search Activity http://code.google.com/p/transdroid/source/browse/trunk/res/layout/search.xml
Используйте Footer для просмотра списка.
list_layout.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px" />
</LinearLayout>
footerview.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</FrameLayout>
и в Activity.java
list=(ListView)findViewById(R.id.list);
FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footerview,null);
btnPostYourEnquiry = (Button) footerLayout.findViewById(R.id.btnGetMoreResults);
list.addFooterView(footerLayout);
Просто укажите "layout_weight = 1" в ListView.
Пример:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:layout_weight="1"
android:dividerHeight="1px" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ff8f40"
android:padding="20dp"
android:text="Click"
android:textColor="#FFFFFF"
android:textSize="22sp"
/>
</LinearLayout>
Я пришел сюда в поисках ответа, но нашел его где-то еще...
Это очень просто, вам просто нужно поместить вес 1 в список внутри линейного макета, другие текстовые/кнопки/и т.д. не должны иметь никакого значения веса.
Конечно, вы можете использовать пользовательский адаптер и указать элемент нижнего колонтитула. Но вы, вероятно, тоже могли бы уйти, положив его внизу ScrollView
и растягивая ListView
по вертикали до содержимого:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_bg">
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ff6a00"
android:divider="#ff8f40"
android:dividerHeight="1px"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="50sp"
android:background="#676767"
android:orientation="vertical">
<Button android:layout_width="100px"
android:layout_height="wrap_content"
android:id="@+id/btnGetMoreResults"
android:layout_marginLeft="10px"
android:text="Get more" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Ну... детали реализации не так просто, если вы хотите создать плавную кнопку "добавить еще" в конце списка. Итак, вот какой код:
myArrayAdapter = new ArrayAdapter<Show>(this, android.R.layout.simple_list_item_1, myList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if( position >= super.getCount() )
return buttonMore ;
MyNormalView view = null;
if (convertView == null || convertView instanceof Button )
view = new MyNormalView(getContext());
else
view = (MyNormalView) convertView;
//customize view here with data
return view;
}
@Override
public int getCount() {
return super.getCount()+1;
}//met
};