2015-10-25 10 views
11

私はcustom viewを作成して、私がLinearLayoutの中に動的に追加しているアクティビティに取り組んでいます。問題は、私が追加するカスタムビューの間にマージンを与えることができないということです。マージンを与えないカスタムビュー

ここでは、私が追加した2つのビューをお互いに見ています。動的ビューを作成するための

enter image description here

コード:

Screentshot

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

あなたのタグのXMLファイルに 'layout_margin'を設定しないのはなぜですか?パディングと同じですか? – Marko

+0

なぜこれが動作するのか分かりませんが、これも前に試しました。とにかく助けてくれてありがとう。これは問題を解決します、 – Mj1992

+0

うれしいことを聞いてうれしいです:)。 – Marko

答えて

2

あなたはパディングでやったのと同じ、あなたのXMLファイルで、あなたのRelativeLayoutlayout_marginを設定してみる必要があります。

<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

脇に追加したいだけです:これを行うと追加のRelativeLayoutが作成されます。たとえば、 'customview_tags_name'は、外のRelativeLayoutの唯一の子である' custom_tag'を持つ腐敗から2つのレベルになります。 – CodyEngel

4

あなたはピクセルにマージンを設定しています。 dpに変換して、余白の値が表示濃度とは無関係になるようにしてください。

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

onLayout(マージンコードを取り除く)と、それらのマージンを設定し、あなたがなどのLinearLayoutまたはRelativeLayoutのようなのViewGroupの内側に、このビューをputingている

+0

こんにちは、ありがとう、この1つは動作していません、私はアプリをデバッグしようとし、デバッガがこの機能で停止していないことがわかった私は動的にタグを追加しようとします。 – Mj1992

+0

onLayout()が呼び出されないことはありません。あなたのコードを見て、どうしてあなたがonLayout()の中にそれらを既に割り当てているのであれば、なぜそれらを再度割り当てているのですか?タグビューの親がRelativeLayoutの場合、RelativeLayoutパラメータを2番目に使用します。私はそれがonLayout()内ではなく、それらを膨らませたら、タグビューにparamsを割り当てる方が良いと思います。 –

+0

私はそれをデバッグしており、呼び出すことはありません。それ以外の場合は、ビューを追加するとそこで停止してしまいます。私は 'Tags'クラスの名前を変更して同じアクティビティを実行しようとすると、同じ問題でスムーズに動作します。 'Tags'アクティビティが使用されていないか、正しく動作していないような気がします。 RelativeLayoutパラメータをどこで使用したいですか?私はそれを取得していない – Mj1992

0

I問題は、コードのこの部分で産むことを考える:

ここ
@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()となります。
この現象を防ぐ1つの方法は、埋め込みをしたときと同じように、tags.xmlファイルに余白を追加することです。無限のレイアウトループがなくなり、ビューに余白を設定する機会があります。

0

Iを動的ビューを設計しており、そのようなあなたのようなパラメータを与えて、このコードを見て:

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
画素使用からDPに変換します

左マージンを追加して追加する必要があります。

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); 
関連する問題