【问题标题】:LinearLayout addview() disables margin on first childLinearLayout addview() 禁用第一个孩子的边距
【发布时间】:2017-05-30 14:04:09
【问题描述】:

我在向LinearLayout 动态添加视图时遇到问题。 我正在尝试从xml 膨胀cardViews。我正在添加 4 CardViews 并且每个视图都可以,除了没有边距的 first 。我不知道为什么。 (下图)

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;
        }
    }
}

activity_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&#36;"
            android:textSize="23sp"
            android:textStyle="bold" />
    </RelativeLayout>
</android.support.v7.widget.CardView>

【问题讨论】:

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


    【解决方案1】:

    代替:

    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);
    

    【讨论】:

    • 谢谢修复它:)
    猜你喜欢
    • 1970-01-01
    • 2016-05-03
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多