简体   繁体   English

无法为相对布局添加边距

[英]Unable to add margin right to relative layout

I trying to add margin right to my relative layout. 我试图在我的相对布局中增加边距。 I need to add it in to HorizontalScrollView and bind data through java code. 我需要将其添加到Horizo​​ntalScrollView并通过Java代码绑定数据。 I have created layout file and add that file using layout inflator. 我已经创建了布局文件,并使用布局充气器添加了该文件。

图片在这里

Below is my code: XML Code: 下面是我的代码: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> 

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: 下面是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);

        }

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. Layout Inflater的inflate方法具有第二个参数,该参数以根视图作为参数。 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. 这样,就无需在最终的父布局中添加放大视图,因为它将自动附加到所提供的布局中,从而保持其空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM