在我的ListView活动中,活动的底部出现了一个大的白色区域,我无法弄清楚为什么以及如何修复它,至于我为什么在这里,它是一个使用arraylist中的对象的listview ,自定义适配器和xml模板,该模板在listview中填充了对象数据。希望这些信息对您有所帮助,感谢您的帮助,因为我完全迷路了。
activity_product_list.xml(列表活动)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ProductList"
android:orientation="vertical">
<include layout="@layout/toolbarnormal"
android:id="@+id/toolbar"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="@+id/lstProducts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true">
</ListView>
</ScrollView>
</LinearLayout>
ProductListLayout.xml(模板)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:background="@color/bodyLight">
<RelativeLayout
android:id="@+id/relProductContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
>
<ImageView
android:id="@+id/imgProductPic"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerHorizontal="true"
android:src="@drawable/placeholder"
android:padding="5dp"
/>
<TextView
android:id="@+id/txtProductName"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Product name"
android:layout_below="@+id/imgProductPic"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:maxLines="2"
android:textAllCaps="false"
android:textAlignment="center"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/txtProductPrice"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="£49.99"
android:layout_below="@+id/txtProductName"
android:layout_centerHorizontal="true"
android:textSize="15sp"
android:layout_marginTop="5dp"
android:maxLines="2"
android:textAlignment="center"
android:textAllCaps="false"
/>
<EditText
android:id="@+id/etProductAmount"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_below="@+id/txtProductPrice"
android:layout_toLeftOf="@+id/btnAddToBasket"
android:layout_alignLeft="@+id/imgProductPic"
android:layout_alignBottom="@id/btnAddToBasket"
android:text="1"
android:textAlignment="center"
/>
<Button
android:id="@+id/btnAddToBasket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to basket"
android:textAllCaps="true"
android:textAlignment="center"
android:textSize="14sp"
android:layout_below="@+id/txtProductPrice"
android:layout_alignRight="@+id/imgProductPic"
android:layout_marginTop="10dp"
/>
</RelativeLayout>
</RelativeLayout>
ProductList.java
package com.example.jack.cbdcalculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
public class ProductList extends AppCompatActivity {
private ImageButton imgbtnBack;
private ListView lstProducts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lstProducts = (ListView) findViewById(R.id.lstProducts);
ArrayList<ProductObject> productObjectArrayList = new ArrayList<ProductObject>();
ProductObject productObject = new ProductObject("Love Hemp™ 800mg 8% CBD Oil – 10ml", "A bottle contains 800MG of CBD Oil – 8% Purity in 10ml",
"Certified Organic Hemp.\n" +
"Non-GMO, Vegan friendly, CO2 Extracted.\n" +
"Fast-Acting.\n" +
"Peppermint flavour available.\n" +
"Sourced from High-CBD Hemp cultivated in the US.\n" +
"Strict GMP Manufacturing & Quality Control process.\n" +
"Public Third-Party Lab Test Results.",
"https://i1.wp.com/benefitcbduk.co.uk/wp-content/uploads/2018/02/what-are-the-benefits-of-cbd.jpg?fit=1200%2C1200&ssl=1", 49.99);
ProductObject productObject2 = new ProductObject("Koi CBD – Multi-Purpose CBD Vape or Tincture Oil (250MG-1000MG)",
"A novel Cannabidiol product from Koi CBD, the Multi-Purpose CBD Vape E-liquid or Tincture CBD Oil. Can’t decide whether to buy Vape or CBD Oil? Why not do both?",
" No Flavoured Additives.\n" +
"> Non-Psychoactive (Zero THC).\n" +
"> Hexane Free.\n" +
"> GMO Free.\n" +
"> Pesticide Free.\n" +
"> Multi-Purpose – Perfect vape juice to add to other e-liquids and ideal for use as a tincture or to add to other foods/drinks.",
"https://i2.wp.com/benefitcbduk.co.uk/wp-content/uploads/2018/06/koi-cbd-250-gold.png?fit=1080%2C1080&ssl=1", 44.99);
productObjectArrayList.add(productObject);
productObjectArrayList.add(productObject2);
ProductAdapter productAdapter = new ProductAdapter(this, R.layout.productlistlayout, productObjectArrayList);
lstProducts.setAdapter(productAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.basket:
startActivity(new Intent(this, Basket.class));
default:
return super.onOptionsItemSelected(item);
}
}
}
ProductObject.java
package com.example.jack.cbdcalculator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
public class ProductList extends AppCompatActivity {
private ImageButton imgbtnBack;
private ListView lstProducts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lstProducts = (ListView) findViewById(R.id.lstProducts);
ArrayList<ProductObject> productObjectArrayList = new ArrayList<ProductObject>();
ProductObject productObject = new ProductObject("Love Hemp™ 800mg 8% CBD Oil – 10ml", "A bottle contains 800MG of CBD Oil – 8% Purity in 10ml",
"Certified Organic Hemp.\n" +
"Non-GMO, Vegan friendly, CO2 Extracted.\n" +
"Fast-Acting.\n" +
"Peppermint flavour available.\n" +
"Sourced from High-CBD Hemp cultivated in the US.\n" +
"Strict GMP Manufacturing & Quality Control process.\n" +
"Public Third-Party Lab Test Results.",
"https://i1.wp.com/benefitcbduk.co.uk/wp-content/uploads/2018/02/what-are-the-benefits-of-cbd.jpg?fit=1200%2C1200&ssl=1", 49.99);
ProductObject productObject2 = new ProductObject("Koi CBD – Multi-Purpose CBD Vape or Tincture Oil (250MG-1000MG)",
"A novel Cannabidiol product from Koi CBD, the Multi-Purpose CBD Vape E-liquid or Tincture CBD Oil. Can’t decide whether to buy Vape or CBD Oil? Why not do both?",
" No Flavoured Additives.\n" +
"> Non-Psychoactive (Zero THC).\n" +
"> Hexane Free.\n" +
"> GMO Free.\n" +
"> Pesticide Free.\n" +
"> Multi-Purpose – Perfect vape juice to add to other e-liquids and ideal for use as a tincture or to add to other foods/drinks.",
"https://i2.wp.com/benefitcbduk.co.uk/wp-content/uploads/2018/06/koi-cbd-250-gold.png?fit=1080%2C1080&ssl=1", 44.99);
productObjectArrayList.add(productObject);
productObjectArrayList.add(productObject2);
ProductAdapter productAdapter = new ProductAdapter(this, R.layout.productlistlayout, productObjectArrayList);
lstProducts.setAdapter(productAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.basket:
startActivity(new Intent(this, Basket.class));
default:
return super.onOptionsItemSelected(item);
}
}
}
ProductAdapter.java
package com.example.jack.cbdcalculator;
public class ProductObject {
String productName, productDescription, productExtraInfo, productImageUrl;
Double productPrice;
public ProductObject(String productName, String productDescription, String productExtraInfo, String productImageUrl, Double productPrice) {
this.productName = productName;
this.productDescription = productDescription;
this.productExtraInfo = productExtraInfo;
this.productImageUrl = productImageUrl;
this.productPrice = productPrice;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDescription() {
return productDescription;
}
public void setProductDescription(String productDescription) {
this.productDescription = productDescription;
}
public String getProductExtraInfo() {
return productExtraInfo;
}
public void setProductExtraInfo(String productExtraInfo) {
this.productExtraInfo = productExtraInfo;
}
public String getProductImageUrl() {
return productImageUrl;
}
public void setProductImageUrl(String productImageUrl) {
this.productImageUrl = productImageUrl;
}
public Double getProductPrice() {
return productPrice;
}
public void setProductPrice(Double productPrice) {
this.productPrice = productPrice;
}
}
答案 0 :(得分:0)
在您的ProductListLayout.xml
中,第一个android:layout_height="match_parent"
声明应该具有一个常数或其他不占用屏幕内容的大小,而不是使属性与父项匹配。现在,您看到的是空白,因为仅显示第一个元素,因为没有其他空间。尝试将其更改为200dp
。