Java LinearLayout addview()禁用第一个子项上的边距
我在将视图动态添加到Java LinearLayout addview()禁用第一个子项上的边距,java,android,xml,android-layout,android-linearlayout,Java,Android,Xml,Android Layout,Android Linearlayout,我在将视图动态添加到LinearLayout时遇到问题。 我正试图从xml中为CardView充气。我添加了4个cardwiews,每个视图都可以,除了第一个没有边距。我不知道为什么。 (下图) MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) {
LinearLayout
时遇到问题。
我正试图从xml
中为CardView充气。我添加了4个cardwiew
s,每个视图都可以,除了第一个没有边距。我不知道为什么。
(下图)
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Select Sale");
ShowSales(getSales());
}
private void ShowSales(Sale[] sales) {
LinearLayout sale_layout = (LinearLayout) findViewById(R.id.sale_layout);
for (Sale sale : sales) {
CardView saleCard = (CardView) getLayoutInflater().inflate(R.layout.sale_card, (ViewGroup) findViewById(R.id.sale_card), false);
((TextView) saleCard.findViewById(R.id.sale_name)).setText(sale.name);
((TextView) saleCard.findViewById(R.id.sale_desc)).setText(sale.desc);
((TextView) saleCard.findViewById(R.id.sale_price)).setText(sale.price + getString(R.string.dollar));
sale_layout.addView(saleCard);
}
}
//temp method to get sales - in future will get sales from server
private Sale[] getSales() {
return new Sale[]{
new Sale("Sale number 1", "desc desc desc desc", 14.90),
new Sale("Sale number 2", "desc desc desc desc", 19.90),
new Sale("Sale number 3", "desc desc desc desc", 25.90),
new Sale("Sale number 4", "desc desc desc desc", 17.90),
};
}
class Sale {
String name, desc;
double price;
public Sale(String name, String desc, double price) {
this.name = name;
this.desc = desc;
this.price = price;
}
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/sale_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:orientation="vertical" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:clickable="true"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
</ScrollView>
sale_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/sale_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
app:cardCornerRadius="@dimen/cardview_default_radius">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/sale_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_margin="8dp"
android:text="Sale name"
android:textSize="27sp" />
<TextView
android:id="@+id/sale_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/sale_name"
android:text="this is description of the sale" />
<TextView
android:id="@+id/sale_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="100$"
android:textSize="23sp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
而不是:
CardView saleCard = (CardView) getLayoutInflater()
.inflate(R.layout.sale_card, (ViewGroup) findViewById(R.id.sale_card), false);
执行:
CardView saleCard = (CardView) getLayoutInflater()
.inflate(R.layout.sale_card, sale_layout, false);
修复它的Thx:)