2015-10-25 32 views
11

pracuję nad działalnością gdzie stworzyłem custom view że dodaję dynamicznie w LinearLayout. Problem polega na tym, że nie mogę podać marginesu między dodanymi widokami niestandardowymi.Widok niestandardowy nie daje marginesu

Oto spojrzenie na dwóch widokach dodałem, które są obok siebie.

Screentshot:

enter image description here

kod do tworzenia widoków dynamicznie:

private void AddSelectedTagUI(final com.main.feedify.serializers.Tags tag){ 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.tags,tags_section_selectedtags,false); 

    TextView tagName = (TextView)v.findViewById(R.id.customview_tags_name); 
    tagName.setText(tag.getTagName()); 

    ImageView close_button = (ImageView)v.findViewById(R.id.customview_tags_close); 

    close_button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RemoveTag(tag.getTagID(), selectedTags); 
     } 
    }); 

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

    tags_section_selectedtags.addView(v, params); 


    // cast view to tags and add it in our layout. 
    // this will help us find out the tag we wanna delete. 

    view_selectedTags.add(v); 
    this.UpdateMessage(true); 
} 

tags.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/tags" 
    android:padding="5dp" 
    android:id="@+id/custom_tag" 
    > 

    <TextView android:id="@+id/customview_tags_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Beyonce" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="3dp" 
     android:layout_centerVertical="true" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/title" /> 

    <ImageView 
     android:id="@+id/customview_tags_close" 
     android:layout_width="wrap_content" 
     android:layout_marginTop="2dp" 
     android:layout_toRightOf="@+id/customview_tags_name" 
     android:layout_height="wrap_content" 
     android:src="@drawable/close" 
     android:layout_marginLeft="3dp" 
     android:layout_marginRight="10dp" 
     /> 

</RelativeLayout> 

activity_tags.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/background"> 

    <TextView 
     android:id="@+id/tags_lbl_taginfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="Add at least 4 favorite topics" 
     android:layout_marginTop="20dp" 
     /> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_below="@+id/tags_lbl_taginfo" 
     android:layout_width="match_parent" 
     android:layout_height="190dp" 
     android:layout_marginTop="15dp" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:id="@+id/tags_section_selectedtags"> 




    </LinearLayout> 


    <EditText 
     android:id="@+id/tags_txt_tags" 
     android:layout_below="@+id/tags_section_selectedtags" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="35dp" 
     android:hint="Add tags ..." 
     android:textColor="@color/tags_edittext" 
     android:paddingTop="15dp" 
     android:singleLine="true" 
     android:paddingLeft="9dp" 
     android:paddingBottom="15dp" 
     android:background="@drawable/tags_edittext" 
     /> 

    <TextView 
     android:id="@+id/tags_lbl_tagsuggestion" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="Suggestions" 
     android:layout_marginTop="15dp" 
     android:layout_below="@id/tags_txt_tags" 
     /> 

</RelativeLayout> 

Niestandardowe tagi Klasa:

package com.main.feedify.custom_views; 

import android.app.Activity; 
import android.content.Context; 
import android.util.AttributeSet; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.main.feedify.feedify_mockup.R; 
import com.tokenautocomplete.TokenCompleteTextView; 
/** 
* Created by Muazzam on 10/17/2015. 
*/ 
public class Tags extends RelativeLayout{ 

    private com.main.feedify.serializers.Tags tag; 

    public Tags(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     inflater.inflate(R.layout.tags,this); 
    } 

    public void setTag(com.main.feedify.serializers.Tags t){ 
     this.tag = t; 
    } 

    public com.main.feedify.serializers.Tags getTag(){ 
     return this.tag; 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     MarginLayoutParams margins = MarginLayoutParams.class.cast(getLayoutParams()); 
     int margin = 5; 
     margins.topMargin = 0; 
     margins.bottomMargin = 0; 
     margins.leftMargin = margin; 
     margins.rightMargin = 0; 
     setLayoutParams(margins); 
    }; 
} 
+1

Dlaczego nie ustawić "layout_margin" w swoim pliku XML dla swojego tagu, tak samo jak w padding? – Marko

+0

Nie jestem pewien, dlaczego zaczął działać, teraz próbowałem tego również. W każdym razie wielkie dzięki za pomoc. To rozwiązuje problem, – Mj1992

+0

Cieszę się, że to działa :). – Marko

Odpowiedz

2

Należy starać ustawienie layout_margin do listy RelativeLayout w pliku XML, tak samo jak ty z wyściółką.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:orientation="horizontal" 
    android:background="@drawable/tags" 
    android:layout_margin="5dp" 
    android:padding="5dp" 
    android:id="@+id/custom_tag"> 

    <TextView android:id="@+id/customview_tags_name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Beyonce" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="3dp" 
     android:layout_centerVertical="true" 
     android:textColor="@android:color/white" 
     android:textSize="@dimen/title" /> 

    <ImageView 
     android:id="@+id/customview_tags_close" 
     android:layout_width="wrap_content" 
     android:layout_marginTop="2dp" 
     android:layout_toRightOf="@+id/customview_tags_name" 
     android:layout_height="wrap_content" 
     android:src="@drawable/close" 
     android:layout_marginLeft="3dp" 
     android:layout_marginRight="10dp" /> 

</RelativeLayout> 
+0

Po prostu chcę dodać na bok: zrobienie tego stworzy dodatkowy RelativeLayout do stworzenia. Na przykład 'customview_tags_name' będzie dwa poziomy z dala od zgnilizny z' custom_tag' będącym jedynym dzieckiem z zewnętrznego RelativeLayout. – CodyEngel

4

ustawiania marginesów w pikseli. Spróbuj przekształcając je w dp, tak że wartość marże staje się niezależna od gęstości wyświetlacza.

@Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     MarginLayoutParams margins = MarginLayoutParams.class.cast(getLayoutParams()); 
     int margin = pxToDp(5); 
     margins.topMargin = 0; 
     margins.bottomMargin = 0; 
     margins.leftMargin = margin; 
     margins.rightMargin = 0; 
     setLayoutParams(margins); 
    }; 

private int pxToDp(int px) { 
     return (int) (px/Resources.getSystem().getDisplayMetrics().density); 
} 

EDIT:

Pozbądź kodu marży w onLayout() i ustawić te marginesy, gdy jesteś oddanie tego widoku wewnątrz viewgroup jak LinearLayout lub RelativeLayout itp

+0

Cześć, dziękuję za odpowiedź, ale ta nie działa, próbowałem debugować aplikację i odkryłem, że debugger nie zatrzymuje się na tej funkcji, gdy Próbuję dynamicznie dodawać etykiety. – Mj1992

+0

Nie jest możliwe funkcja onLayout() nie jest wywoływana. Patrząc na swój kod, dlaczego przypisujesz te parametry ponownie, jeśli już je przypisałeś w onLayout(). Drugie użycie parametrów RelativeLayout, jeśli rodzicem widoku tagu jest RelativeLayout. Myślę, że lepiej jest przypisać parametry do widoku znacznika, gdy je zawyłasz, a nie w trybie onLayout(). –

+0

Jestem debugowania i nie dzwoni. W przeciwnym razie zatrzymałbym się, gdy dodaję widok. Następnie zmieniłem nazwę mojej klasy "Tagi" i próbuję uruchomić to samo działanie i działa ona płynnie z tymi samymi problemami. Czuję, że działanie 'Tagi' nie jest używane lub nie odgrywa ono właściwie roli. Gdzie mam używać paramatur RelativeLayout? Nie dostałem tego – Mj1992

0

I myślę, że problem polega na tym kawałku kodu:

@Override 
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
    MarginLayoutParams margins = MarginLayoutParams.class.cast(getLayoutParams()); 
    int margin = 5; 
    margins.topMargin = 0; 
    margins.bottomMargin = 0; 
    margins.leftMargin = margin; 
    margins.rightMargin = 0; 
    setLayoutParams(margins); 
}; 

Tutaj zmieniasz parametry układu wewnątrz onLayout() metoda i powinno prowadzić do nieskończonej układ widoku: rodzic widok widoku niestandardowego za nazywa onLayout() który nazywa setLayoutParams() co powoduje onLayout() ... I tak dalej.
Jednym ze sposobów, aby zapobiec tego zachowania to dodać marginesy w tags.xml pliku, tak jak to zrobiłeś z uszczelek. Nieskończona pętla układu zniknie, a widok będzie miał szansę ustawić swoje marginesy.

0

Mam zaprojektowane widok dynamicznie i dały parametry takie jak twoje, spojrzeć na ten kod:

https://stackoverflow.com/a/33339420/5479863

i przekształcenia się dp z użycia piksela metoda ta

private int getPixelsToDP(int dp) { 
    float scale = getResources().getDisplayMetrics().density; 
    int pixels = (int) (dp * scale + 0.5f); 
    return pixels; 
} 
1

Powinieneś dodać lewy margines, aby zobaczyć podczas dodawania.

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
//add this line 
    params.leftMargin=50; 
//end 
    tags_section_selectedtags.addView(v, params); 
Powiązane problemy