如何在LinearLayout中实现ListView?

时间:2014-06-09 04:23:17

标签: java android android-layout android-listview android-linearlayout

我正在尝试创建一个简单的Checkbook应用程序,其MainActivity存储一个事务列表。我希望屏幕顶部和底部的TextView分别显示帐户余额和添加新事务的选项。我想要一个滚动之间的事务列表。我能够实现ListView并添加页眉和页脚视图,但如果事务列表超出屏幕大小,页眉和页脚可以滚动屏幕。

有没有办法在线性布局中定位ListView,或者冻结页眉/页脚以保持在屏幕上?

到目前为止,这是我的XML文件:     

<TextView
    android:id="@+id/header_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/default_header_string">
</TextView>

<ListView
    android:id="@+id/transaction_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</ListView>

<TextView
    android:id="@+id/footer_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/add_transaction_string">
</TextView>

这是我的onCreate,没有语法错误,但我无法点击footerview添加交易:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checkbook);

    ListView list = (ListView) findViewById(R.id.transaction_list_view);

    // Create a new Adapter
    mAdapter = new TransactionAdapter(list.getContext());

    // Inflate footerView and headerView
    LayoutInflater inflater = getLayoutInflater();
    TextView headerView = (TextView) inflater.inflate(R.layout.header_view, null);
    TextView footerView = (TextView) inflater.inflate(R.layout.footer_view, null);

    // Set listener for footerView
    footerView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent transactionIntent = new Intent(CheckbookActivity.this, AddTransactionActivity.class);
            startActivityForResult(transactionIntent, ADD_TRANSACTION_REQUEST);
        }
    });

    list.setAdapter(mAdapter);
}

6 个答案:

答案 0 :(得分:2)

使用以下代码。这将满足您的要求。我试过这个并为我工作。 具有以下属性的相对布局。 Relativelayout比使用权重方法的线性布局更好。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true"
    android:gravity="center_horizontal"
    android:text="ListView Heading" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:gravity="center_horizontal"
    android:text="ListView Footer" />

<ListView 
     android:id="@+id/listView1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_below="@id/textView1"
     android:layout_above="@id/textView2"

    ></ListView>

用户界面会喜欢这个

enter image description here

答案 1 :(得分:1)

是的,您可以在linearlayout中使用权重和layout_weight进行操作,也可以使用RelativeLayout创建此类视图。

1)在LinearLayout中,只需将weightsum="1"添加到您的linearlayout并将layout_weight="0.2"添加到每个页眉和页脚,然后将layout_weight="0.6"添加到列表视图中。

2)在relativeLayout中,将alignParentTop添加到标题,将alignParentBottom添加到页脚,并将listview设置为layout_below="@+id/header"layout_above="2+id/footer"

答案 2 :(得分:1)

试试这种方式,希望这可以帮助您解决问题。 而不是使用页眉/页脚,只需在XML中添加以下代码:

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

    <TextView
        android:id="@+id/header_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/default_header_string">
    </TextView>

    <ListView
        android:id="@+id/transaction_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ListView>

    <TextView
        android:id="@+id/footer_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_transaction_string">
    </TextView>
</LinearLayout>

答案 3 :(得分:0)

我从类似的帖子中找到了possible solution来解决您的问题。希望这会对你有所帮助。

答案 4 :(得分:0)

您想要完成什么来冻结页眉/页脚。使用相对布局来定位页眉/页脚然后将列表视图放在中间

会更容易
<RelativeLayout ...>
<TextView
    android:id="@+id/header_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:gravity="center"
    android:text="@string/default_header_string">
</TextView>

<ListView
    android:id="@+id/transaction_list_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header_view"
    android:layout_above="@+id/footer_view">
</ListView>

<TextView
    android:id="@+id/footer_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:text="@string/add_transaction_string">
</TextView>
</RelativeLayout>

您可以使用LinearLayout执行此任务。但我不推荐它,因为它有点&#34; hacky&#34;。

答案 5 :(得分:0)

  1. 获取数组中的所有元素:示例: - (weatherArray)

  2. 遍历所有元素: -

  3. 示例: -

    mainLayout = ((LinearLayout)refreshObj.get("mainLayout"));
        mainLayout.removeAllViews();
    
    for (int i = 0; i < weatherArray.length(); i++) {
    
     View childView = getLayoutInflater().inflate(R.layout.weather_row4_item,   mainLayout,false); 
    
     TextView todayTempStatus = (TextView) childView.findViewById(R.id.todayTempStatus);
    
    todayTempStatus.setText("");
    
    }
    
    1. 这是一个不使用listview的示例,我们将使用子视图填充lienarlayout。