我试图为我的相对布局添加边距。我需要将它添加到HorizontalScrollView并通过java代码绑定数据。我创建了布局文件并使用布局充气器添加该文件。
以下是我的代码: XML代码:
<HorizontalScrollView
android:id="@+id/propertyListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:background="@color/white"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:id="@+id/propertyListViewContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
下面是布局文件。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginRight="20dp">
<ImageView
android:id="@+id/propertyImage"
android:layout_width="280dp"
android:layout_height="190dp"
android:scaleType="fitXY"
android:src="@drawable/image_ad_property" />
<RelativeLayout
android:id="@+id/propertyPriceContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/propertyImage"
android:background="#f2f2f2"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="₹ 1.25 cr onwards"
android:textColor="#6a6a6a"
android:textSize="12sp" />
</RelativeLayout>
<TextView
android:id="@+id/propertyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/propertyPriceContainer"
android:layout_marginTop="10dp"
android:text="Park Royale"
android:textColor="#353535"
android:textSize="14sp" />
<TextView
android:id="@+id/propertyPlace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/propertyName"
android:layout_marginTop="4dp"
android:text="Gokhale Marg"
android:textColor="#6b6b6b"
android:textSize="13sp" />
<LinearLayout
android:id="@+id/rateContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/propertyPriceContainer"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/ratingImage1"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/icon_rating_selected" />
<ImageView
android:id="@+id/ratingImage2"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/icon_rating_selected" />
<ImageView
android:id="@+id/ratingImage3"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/icon_rating_selected" />
<ImageView
android:id="@+id/ratingImage4"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/icon_rating_normal" />
<ImageView
android:id="@+id/ratingImage5"
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@drawable/icon_rating_normal" />
</LinearLayout>
<LinearLayout
android:id="@+id/varifiedContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/rateContainer"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<ImageView
android:layout_width="12dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:src="@drawable/icon_verified" />
<TextView
android:id="@+id/homeVerified"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="Verified"
android:textColor="#6b6b6b"
android:textSize="13sp" />
</LinearLayout>
</RelativeLayout>
以下是java文件:
LinearLayout propertyListViewContainer = (LinearLayout) findViewById(R.id.propertyListViewContainer);;
for (int i = 0; i < 10; i++) {
RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, null);
final float scale = getResources().getDisplayMetrics().density; //set height and width
int dpWidthInPx = (int) (280 * scale); //set width
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsBuyRent.setMargins(0, 0, 20, 0);
child.setLayoutParams(paramsBuyRent);
propertyListViewContainer.addView(child);
}
答案 0 :(得分:0)
更改:
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT);
为:
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
OR
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
答案 1 :(得分:0)
布局inflater的inflate方法有第二个参数,它以根视图作为参数。如果您不提供,则将忽略边距。因此,将容器视图作为第二个参数提供。像这样:
for (int i = 0; i < 10; i++) {
RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, propertyListViewContainer);
}
这样就不需要在最后将膨胀的视图添加到父布局,因为它会自动附加到提供的布局,保持其边距不变。