wsa*_*lle 9 android android-layout
我正在努力让布局看起来正确,我试图生成我问题的最短,最小可能的例子.
我的目标是在屏幕的顶部和底部有一个页眉和页脚视图,ListView在两者之间,使用另一个视图(让我们称之为标签,它是屏幕截图中的灰色框)直接位于屏幕下方ListView.ListView需要滚动时,应始终显示此标签和页脚.
当ListView不需要滚动时(这是正确的):

当ListView需要滚动时,页脚和灰色框被推离屏幕(错误):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"/>
<!-- Used to push the footer to the bottom -->
<View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
public class TestActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> items = new ArrayList<String>();
items.add("one");
items.add("two");
items.add("three");
items.add("four");
items.add("five");
items.add("six");
items.add("seven");
items.add("eight");
items.add("nine");
items.add("ten");
setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, items));
setContentView(com.myproject.android.R.layout.test);
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了几种不同的方法,例如给出ListView layout_weight="1"和删除View我用来将页脚推到底部的空白.这几乎是我想要的,它在ListView滚动时保持页脚和标签可见,但是当它只有1或2个项目时,我需要在它下方的灰色框ListView.我也尝试过使用a RelativeLayout,但没有成功.我想我完全误解了事情.
这是我的尝试RelativeLayout仍然是不正确的.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"
android:id="@+id/header"
/>
<ListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"
android:layout_below="@id/header"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll"
android:padding="5dp"
android:background="@color/light_gray"
android:textColor="@color/black"
android:layout_below="@android:id/list"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"
android:layout_alignParentBottom="true"
android:id="@+id/footer"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

添加android:layout_weight="1"到列表视图.这将使它成为LinearLayout中最大的元素,而不会将其他元素从屏幕上移开.
小智 7
此布局将标题添加到屏幕的顶部以及脚注和底部.该列表填充了屏幕的其余部分.随着这些aproach列表元素永远不会被页脚遮挡.了解如何在XML下面添加灰色框...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="header"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/red"
android:id="@+id/header"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="footer"
android:padding="20dp"
android:textSize="18sp"
android:background="@color/blue"
android:layout_alignParentBottom="true"
android:id="@+id/footer"/>
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/list"
android:layout_below="@id/header"
android:layout_above="@id/footer"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
好.此XML解决了页脚丢失的问题.现在我们必须在列表的末尾添加一个灰色框.我认为有两种方法可以做到:
使用addFooterView方法:http://developer.android.com/reference/android/widget/ListView.html#addFooterView ( android.view.View )
使用适配器和getViewTypeCount()方法进行游戏,以便定义两种类型或元素:普通元素和页脚元素.http://developer.android.com/reference/android/widget/BaseAdapter.html#getViewTypeCount()
| 归档时间: |
|
| 查看次数: |
8576 次 |
| 最近记录: |