[英]How can I implement a ListView within a LinearLayout?
I am trying to make a simple Checkbook app, whose MainActivity stores a list of transactions. 我正在尝试制作一个简单的Checkbook应用,其MainActivity存储事务列表。 I would like a TextView at the top and bottom of the screen that show the account balance and an option to add a new transaction, respectively. 我想要在屏幕顶部和底部分别显示帐户余额和添加新交易的选项的TextView。 I would like a list of transactions in between that scroll. 我想要一个滚动列表之间的交易清单。 I was able to implement a ListView and add a header and footer view, but if the transaction list exceeds the size of the screen the headers and footers can scroll off screen. 我能够实现ListView并添加页眉和页脚视图,但是如果事务列表超过了屏幕的大小,则页眉和页脚可以在屏幕外滚动。
Is there any way to position a ListView within the linear layout, or freeze the headers/footers to stay on the screen? 有什么方法可以将ListView放置在线性布局中,还是冻结页眉/页脚以保留在屏幕上?
Here is my XML file so far: 到目前为止,这是我的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>
And here is my onCreate, which has no syntax errors but I am unable to click the footerview to add a transaction: 这是我的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);
}
use the below code. 使用下面的代码。 This will satisfy your requirement. 这将满足您的要求。 I tried this and working for me. 我尝试过并为我工作。 Relative layout with below,above attributes. 具有以下,以上属性的相对布局。 Relativelayout is better than Linear layout with weight method. 相对布局优于采用权重方法的线性布局。
<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>
The UI will like this 用户界面会喜欢这样
Yes, you can do it with weightsum and layout_weight
in linearlayout and also you can create this type of view using RelativeLayout. 是的,您可以在linearlayout中使用weightsum和layout_weight
做到这一点,也可以使用RelativeLayout创建这种类型的视图。
1) In LinearLayout just add weightsum="1"
to your linearlayout and add layout_weight="0.2"
to each of your header and footer and add layout_weight="0.6"
to your listview. 1)在LinearLayout中,只需将weightsum="1"
添加到linearlayout中,然后将layout_weight="0.2"
添加到页眉和页脚中,然后将layout_weight="0.6"
添加到列表视图中。
2) In relativeLayout add alignParentTop to your header and alignParentBottom to your footer and set listview to layout_below="@+id/header"
and layout_above="2+id/footer"
2)在relativeLayout中,将alignParentTop添加到标题中,并将alignParentBottom添加至页脚,并将listview设置为layout_below="@+id/header"
和layout_above="2+id/footer"
Try this way, hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。 Instead of using header/footer just put as below code in your XML: 无需使用页眉/页脚,只需将以下代码放入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>
I found a possible solution for your problem from a similiar post. 我从类似的帖子中找到了可能解决您问题的方法。 Hope this helps you. 希望这对您有所帮助。
For what you are trying to accomplish to freeze the header/footer. 对于您要完成的工作,冻结页眉/页脚。 It will be easier to use a relative layout to position the header/footer then have your listview in the middle 使用相对布局放置页眉/页脚,然后将列表视图放在中间会更容易
<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>
You can use a LinearLayout for this task. 您可以将LinearLayout用于此任务。 But I don't recommend it as it's a bit "hacky". 但是我不建议这样做,因为它有点“ hacky”。
Get all the elements in a array: Example:- (weatherArray) 获取数组中的所有元素:示例:-(weatherArray)
Loop through all the elements :- 遍历所有元素:-
Example:- 例:-
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("");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.