Android - 添加列表片段时隐藏按钮元素

时间:2014-12-28 08:20:11

标签: android android-layout android-fragments

我正在尝试编写一个布局,其中包含Android布局中的列表视图和按钮。列表视图是一个片段,按钮是布局的一部分。

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <!-- fragment container to display transactions for this budget -->
    <LinearLayout
        android:id="@+id/transactionsContainer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/newTransaction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_new_transaction"/>

</LinearLayout>

listview_fragment.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:padding="10dp"
              android:layout_height="wrap_content">

    <TextView
        android:id="@+id/transactionTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="6dp"
        android:layout_toLeftOf="@+id/transactionAmount"
        android:layout_toStartOf="@+id/transactionAmount"
        style="@style/ListItemDescription"/>

    <TextView
        android:id="@+id/transactionAmount"
        style="@style/AmountText"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"/>

</RelativeLayout>

我正在添加这样的片段;

final FragmentManager fm = getFragmentManager();
Fragment container = fm.findFragmentById(R.id.transactionsContainer);

if (container == null) {
    final TransactionListFragment fragment = new TransactionListFragment();
    fm.beginTransaction().add(R.id.transactionsContainer, fragment).commit();
}

片段被添加到布局中,但是,当列表长度大于显示高度时,按钮将丢失。如果列表视图项很少,则按钮可见;

按钮可见3项

enter image description here

包含10个项目的按钮

enter image description here

使用hierarchyviewer调试视图时,Button存在,但看起来好像它位于listview片段后面;

enter image description here

这里有什么想法?

2 个答案:

答案 0 :(得分:1)

使用layout_weight属性

<?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">

    <!-- fragment container to display transactions for this budget -->
    <LinearLayout
        android:id="@+id/transactionsContainer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

    <Button
        android:id="@+id/newTransaction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button_new_transaction"/>a

</LinearLayout>

答案 1 :(得分:0)

给你的布局TransactionsContainer赋予一个权重,这样你就可以强制它在多个项目的情况下不填满整个高度,并将它包装在ScrollView中以滚动项目,同时有很多项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="10">

    <!-- fragment container to display transactions for this budget --> 
    <ScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>
    <LinearLayout
        android:id="@+id/transactionsContainer"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/newTransaction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button_new_transaction"/>
    </LinearLayout>
  </ScrollView>
</LinearLayout>

在运行期间向ScrollView添加视图时:

transactionsContainer.addView(view, 0);