2015-10-25 10 views
11

मैं ऐसी गतिविधि पर काम कर रहा हूं जहां मैंने custom view बनाया है जिसे मैं LinearLayout के भीतर गतिशील रूप से जोड़ रहा हूं। समस्या यह है कि मैं अपने द्वारा जोड़े गए कस्टम विचारों के बीच मार्जिन देने में असमर्थ हूं।कस्टम व्यू मार्जिन

यहां दो विचारों पर एक नज़र डाली गई है जो मैंने एक दूसरे के बगल में हैं।

Screentshot:

गतिशील दृश्य बनाने के लिए enter image description here

कोड:

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> 

कस्टम टैग कक्षा: पिक्सल में

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

क्यों आपके टैग के लिए अपने एक्सएमएल फ़ाइल में 'layout_margin' सेट नहीं, एक ही है कि आप गद्दी के साथ किया था? – Marko

+0

मुझे यकीन नहीं है कि यह अभी क्यों काम करना शुरू कर दिया है मैंने इससे पहले भी कोशिश की। वैसे भी मदद के लिए बहुत बहुत धन्यवाद। यह समस्या हल करता है, – Mj1992

+0

यह सुनकर खुशी हुई कि यह काम कर रहा है :)। – Marko

उत्तर

2

आप layout_margin के लिए सेटिंग की कोशिश करनी चाहिए अपने अपने एक्सएमएल फ़ाइल में RelativeLayout, एक ही आप गद्दी के साथ किया था के रूप में।

<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

बस एक तरफ जोड़ना चाहते हैं: ऐसा करने से एक अतिरिक्त सापेक्ष लेटआउट बनाया जाएगा। उदाहरण के लिए 'customview_tags_name' रोट से दो स्तर दूर होगा' custom_tag' बाहरी सापेक्ष Layout का एकमात्र बच्चा होने के साथ। – CodyEngel

4

आप सेट कर रहे हैं मार्जिन। उन्हें डीपी में परिवर्तित करने का प्रयास करें, ताकि मार्जिन मान प्रदर्शन घनत्व से स्वतंत्र हो जाए।

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

संपादित करें:

onLayout (मार्जिन कोड से छुटकारा) और उन मार्जिन निर्धारित करते हैं, जब आप LinearLayout या RelativeLayout आदि की तरह एक viewgroup अंदर इस दृश्य डाल रहे हैं

+0

हाय, उत्तर के लिए धन्यवाद, लेकिन यह काम नहीं कर रहा है, मैंने ऐप को डीबग करने का प्रयास किया और पता चला कि डीबगर इस फ़ंक्शन पर रोक नहीं रहा है मैं गतिशील रूप से एक टैग जोड़ने की कोशिश करता हूं। – Mj1992

+0

यह संभव नहीं है Layout() को कॉल नहीं किया जाता है। अपने कोड को देखते हुए, यदि आप पहले से ही इनआउटआउट() में असाइन कर चुके हैं तो आप पैरा को फिर से क्यों असाइन कर रहे हैं। दूसरा उपयोग रिलेवेटिवआउट पैराम्स यदि टैग व्यू के पैरेंट रिलेटिव लयआउट है। मुझे लगता है कि टैग दृश्य में पैरा को असाइन करना बेहतर होगा, एक बार जब आप उन्हें फुलाएंगे, तो Layout() के अंदर नहीं। –

+0

मैं इसे डिबग कर रहा हूं और यह कॉल नहीं कर रहा है। अन्यथा जब मैं एक दृश्य जोड़ता तो वहां रुक जाता। मैंने अपना 'टैग' वर्ग भी बदल दिया है और उसी गतिविधि को चलाने की कोशिश की है और यह एक ही समस्या के साथ सुचारू रूप से चलता है। मुझे लगता है कि 'टैग' गतिविधि का उपयोग नहीं किया जा रहा है या इसकी भूमिका ठीक से नहीं खेल रहा है। आप मुझे रिलेवेटिवआउट पैराम्स का उपयोग कहां चाहते हैं? मुझे यह नहीं मिला – Mj1992

0

मैं सोचें कि समस्या कोड के इस टुकड़े में है:

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

यहां आप 0 के अंदर लेआउट पैरामीटर बदलते हैंविधि और इससे आपके दृश्य के अनंत लेआउट का कारण बनना चाहिए: कस्टम व्यू के मूल दृश्य onLayout() पर कॉल करते हैं जो setLayoutParams() पर कॉल करते हैं जिसके परिणामस्वरूप onLayout() ... और इसलिए यह जाता है।
इस व्यवहार को रोकने का एक तरीका है tags.xml फ़ाइल में अपने मार्जिन को जोड़ना, जैसा कि आपने पैडिंग के साथ किया था। अनंत लेआउट लूप चलेगा और आपके दृश्य को इसके मार्जिन सेट करने का मौका मिलेगा।

0

मैं एक दृश्य के गतिशील डिजाइन किया गया है और इस तरह आपके जैसे मानकों को दे दिया है, इस कोड पर एक नजर है:

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

और पिक्सेल उपयोग से डी पी में परिवर्तित करने के लिए इस विधि का

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

आपको इसे जोड़ने के दौरान देखने के लिए बाएं हाशिए को जोड़ना चाहिए।

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); 
संबंधित मुद्दे