2015-10-25 18 views
11

Tôi đang làm việc trên một hoạt động mà tôi đã tạo một custom view mà tôi đang thêm động trong một số LinearLayout. Vấn đề là tôi không thể cung cấp số dư giữa các chế độ xem tùy chỉnh mà tôi thêm vào.Chế độ xem tùy chỉnh không cho lề

Dưới đây là xem hai chế độ xem tôi đã thêm bên cạnh nhau.

Screentshot:

enter image description here

Mã để tạo các quan điểm tự động:

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> 

Tuỳ Thẻ Class: lề

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

Tại sao không đặt 'layout_margin' trong tệp XML cho thẻ của bạn, giống như bạn đã làm với đệm? – Marko

+0

Tôi không chắc chắn lý do tại sao nó bắt đầu làm việc bây giờ tôi đã thử điều này trước khi quá. Dù sao cảm ơn rất nhiều cho sự giúp đỡ. Điều này giải quyết vấn đề, – Mj1992

+0

Vui mừng khi biết điều đó đang hoạt động :). – Marko

Trả lời

2

Bạn nên cố gắng thiết lập các layout_margin để bạn RelativeLayout trong tập tin XML của bạn, giống như bạn đã làm với các padding.

<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

Chỉ muốn thêm một sang một bên: làm điều này sẽ tạo ra một RelativeLayout bổ sung được tạo ra. Ví dụ 'customview_tags_name' sẽ là hai cấp độ đi từ thối với' custom_tag' là con duy nhất của RelativeLayout bên ngoài. – CodyEngel

4

Bạn đang thiết trong Pixels. Hãy thử chuyển đổi chúng thành dp, để giá trị lề trở nên độc lập với mật độ hiển thị.

@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:

Loại bỏ mã lề trong onLayout() và thiết lập những lề, khi bạn đang puting quan điểm này bên trong một ViewGroup như LinearLayout hoặc RelativeLayout, vv

+0

Xin chào, cảm ơn câu trả lời, nhưng cái này không hoạt động, tôi đã cố gắng gỡ lỗi ứng dụng và phát hiện ra trình gỡ lỗi không dừng lại trên chức năng này tôi cố gắng thêm một thẻ động. – Mj1992

+0

Nó không thể onLayout() không được gọi. Nhìn vào mã của bạn, Tại sao bạn lại gán các tham số nếu bạn đã gán chúng trong onLayout(). Thứ hai sử dụng các tham số RelativeLayout nếu cha mẹ của giao diện thẻ là RelativeLayout. Tôi nghĩ tốt hơn là chỉ định các thông số cho chế độ xem thẻ khi bạn đã tăng cường chúng, chứ không phải bên trong onLayout(). –

+0

Tôi đang gỡ lỗi và nó không gọi. Nếu không nó sẽ dừng lại ở đó khi tôi thêm một cái nhìn. Hơn nữa tôi cũng đổi tên lớp 'Tags' của mình và cố gắng chạy cùng một hoạt động và nó chạy trơn tru với cùng một vấn đề. Tôi cảm thấy hoạt động 'Thẻ' không được sử dụng hoặc không phát đúng vai trò của nó. Bạn muốn tôi sử dụng thông số RelativeLayout ở đâu? Tôi đã không nhận được rằng – Mj1992

0

tôi nghĩ rằng vấn đề đặt trong đoạn mã này:

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

Tại đây bạn thay đổi thông số bố cục bên trong 0 Phương phápvà nó sẽ dẫn đến bố cục vô hạn của chế độ xem của bạn: chế độ xem cha của chế độ xem tùy chỉnh gọi số onLayout() gọi số setLayoutParams() dẫn đến onLayout() ... Và như vậy sẽ xảy ra.
Một cách để ngăn chặn hành vi này là thêm lề của bạn vào tệp tags.xml, giống như bạn đã làm với các phần đệm. Vòng lặp bố trí vô hạn sẽ biến mất và chế độ xem của bạn sẽ có cơ hội đặt lề của nó.

0

tôi đã thiết kế một cái nhìn năng động và đã đưa ra các thông số như vậy giống như bạn, có một cái nhìn tại mã này:

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

và chuyển đổi thành dp việc sử dụng điểm ảnh phương pháp này

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

Bạn nên thêm lề trái để xem trong khi thêm nó.

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); 
Các vấn đề liên quan