I trying to add margin right to my relative layout. I need to add it in to HorizontalScrollView and bind data through java code. I have created layout file and add that file using layout inflator.
Below is my code: XML Code:
<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>
Below is layout file.
<?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>
Below is java file:
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);
}
Change :
RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT);
to:
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);
Layout inflater's inflate method has a second parameter that takes root view as a parameter. If you don't provide it the margins will be ignored. So provide container view as the second parameter there. Something like this:
for (int i = 0; i < 10; i++) {
RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, propertyListViewContainer);
}
This way there's no need to add the inflated view to the parent layout at the end as it will automatically be attached to the provided layout keeping it's margins intact.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.