我在这里是因为我想多次使用相同的cardView布局,只更改其中包含的一些信息,然后将其添加到主xml布局中包含的视图中。 问题是附加创建的cardViews的视图是PercentRelativeLayout,所以我需要使用Percent params指定cardView的xml布局。
我向您展示了我尝试过的一个例子(没有成功),以使其更清晰。
main_layout.xml
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textview"
percent:layout_heightPercent="85%"
percent:layout_marginTopPercent="0.5%">
<android.support.percent.PercentRelativeLayout
android:id="@+id/scrollview_prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
...here I want to add views programmatically...
</android.support.percent.PercentRelativeLayout>
...
cardView_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
然后,这是我写的一段Java代码(在一个片段中,但现在没关系)
myfragment.java
...
CardView cv = (CardView)
LayoutInflater.from(getActivity()).inflate(R.layout.cardView_layout, null)
cv.setId(currentId++);
PercentRelativeLayout prl = (PercentRelativeLayout)
view.findViewById(R.id.scrollview_prl);
prl.addView(cv);
...
正如你所看到的,我只是保留一个currentId变量来为我要创建的cardviews分配不同的id。 显然,这些cardview必须是一个在另一个之下,我甚至不知道如何以编程方式将这个其他参数添加到它们。
我想获得的最终布局是这样的:
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textview"
percent:layout_heightPercent="85%"
percent:layout_marginTopPercent="0.5%">
<android.support.percent.PercentRelativeLayout
android:id="@+id/scrollview_prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/firstcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/secondcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@id/thirdcardview"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
....
</android.support.percent.PercentRelativeLayout>
...
提前感谢您的帮助。
答案 0 :(得分:0)
问题出在您的inflate()
电话中。您必须传递parent
值才能正确解释膨胀视图的LayoutParams。
请改为尝试:
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cardView_layout, prl, false);
通过将prl
(PercentRelativeLayout)传递给inflate()
,一切都应该有效。