2016-07-01 8 views
0

Ich versuche, einen Rand zu meinem relativen Layout hinzuzufügen. Ich muss es in HorizontalScrollView hinzufügen und Daten über Java-Code binden. Ich habe eine Layoutdatei erstellt und diese Datei mit dem Layout-Inflator hinzugefügt.Es konnte kein Rand rechts zum relativen Layout hinzugefügt werden.

Image here

Unten ist mein 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> 

Im Folgenden finden Sie die Layout-Datei.

<?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> 

Im Folgenden finden Sie Java-Datei:

 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); 

     } 

Antwort

0

Wechsel:

RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(dpWidthInPx, RelativeLayout.LayoutParams.WRAP_CONTENT); 

zu:

RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

ODER

RelativeLayout.LayoutParams paramsBuyRent = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
0

Die Inflate-Methode des Layout-Inflators hat einen zweiten Parameter, der die Root-Ansicht als Parameter verwendet. Wenn Sie es nicht angeben, werden die Ränder ignoriert. Stellen Sie daher dort die Containeransicht als zweiten Parameter zur Verfügung. Etwas wie folgt aus:

for (int i = 0; i < 10; i++) { 
     RelativeLayout child = (RelativeLayout) getLayoutInflater().inflate(R.layout.layout_project_details_property_item, propertyListViewContainer); 
    } 

Auf diese Weise gibt es keine Notwendigkeit, den aufgeblähten Blick auf das übergeordnete Layout am Ende hinzuzufügen, da sie automatisch auf die vorgesehene Layout angebracht wird es Ränder intakt zu halten.

+0

Hinzugefügt unten Code funktioniert immer noch nicht. RelativeLayout child = (RelativLayout) getLayoutInFlater(). Inflate (R.layout.layout_project_details_property_item, propertyListViewContainer, false); – user3563459

+0

machen dritten Parameter wahr –

Verwandte Themen