我在这里是因为我想多次使用相同的 cardView 布局,只需更改其中包含的一些信息,然后将其添加到主 xml 布局中包含的视图中。问题是附加创建的cardViews的视图是PercentRelativeLayout,所以我需要使用Percent参数指定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代码(在一个片段中,但现在这无关紧要)
我的片段.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 变量来为我要创建的卡片视图分配不同的 ID。显然,这些卡片视图必须在另一个之下,我什至不知道如何以编程方式将另一个参数添加到它们。
我想获得的最终布局是这样的:
<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>
...
在此先感谢您的帮助。