2014-09-19 3 views
1

Я прочитал несколько ответов о том, как устанавливать поля текстового вида программно, когда родительский элемент LinearLayout, но в моем случае мне нужно установить поле текстового представления в relativelayoutКак установить маргинальный текст текстового представления в относительном макете

вот что я пытался и не получилось:

описание моей TextView в следующем коде:

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     params1.addRule(RelativeLayout.RIGHT_OF, idsArray[i]); 
     params1.addRule(RelativeLayout.LEFT_OF, R.id.imageButtonCamera); 
     params1.addRule(RelativeLayout.BELOW, R.id.divider); 
     params1.setMargins(8, 8, 8, 8) 

     description.setLayoutParams(params1); 

Любой намек?

UDPATE:

мой макет XML файл:

TextView с идентификатором textViewTip является один мне нужно изменить относительные и отступы атрибуты.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/item_layout_main" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:background="@drawable/layer_car_background" > 

<TextView 
    android:id="@+id/textViewItemTitle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginTop="12dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginBottom="12dp" 
    android:text="Large Text" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<ImageView 
    android:id="@+id/imageViewTitle" 
    android:layout_width="32dp" 
    android:layout_height="32dp" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@+id/textViewItemTitle" 
    android:src="@drawable/ic_action_expand" /> 

<RelativeLayout 
    android:id="@+id/relativeLayoutExpandable" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="16dp" 
    android:animateLayoutChanges="true" 
    android:layout_below="@+id/textViewItemTitle" > 

    <View 
     android:id="@+id/divider" 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:background="@android:color/darker_gray" /> 

    <ImageView 
     android:layout_below="@id/divider" 
     android:id="@+id/imageViewItem" 
     android:layout_width="100dp" 
     android:layout_height="133dp" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textViewTip" 
     android:textSize="12sp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageViewItem" 
     android:layout_alignTop="@+id/imageViewItem" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="22dp" 
     android:layout_marginBottom="22dp" 
     android:layout_toLeftOf="@+id/imageButtonCamera" 
     android:layout_toRightOf="@+id/imageViewItem" 
     android:text="TextView" /> 

    <EditText 
     android:id="@+id/editTextDescription" 
     android:layout_width="wrap_content" 
     android:layout_height="100dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/textViewTip" 
     android:layout_marginTop="8dp" 
     android:ems="10" 
     android:hint="@string/enter_own_description" 
     android:inputType="textMultiLine" > 

     <requestFocus /> 
    </EditText> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/imageView1" 
     android:layout_marginRight="12dp" 
     android:src="@drawable/ic_ok" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/imageView2" 
     android:layout_below="@+id/imageView2" 
     android:layout_marginTop="8dp" /> 

    <CheckBox 
     android:id="@+id/checkBox2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/checkBox1" 
     android:layout_alignBottom="@+id/checkBox1" 
     android:layout_alignRight="@+id/imageView1" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="32dp" 
     android:layout_height="32dp" 
     android:layout_below="@+id/editTextDescription" 
     android:layout_marginRight="17dp" 
     android:layout_toLeftOf="@+id/imageView2" 
     android:src="@drawable/ic_repair" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/checkBox2" 
     android:layout_alignBottom="@+id/checkBox2" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="8dp" 
     android:text="@string/estimated_cost" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/editTextEstCost" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/checkBox1" 
     android:layout_toLeftOf="@+id/checkBox2" 
     android:layout_toRightOf="@+id/textView1" 
     android:layout_marginRight="24dp" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

    <ImageButton 
     android:id="@+id/imageButtonCamera" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textViewTip" 
     android:background="@android:color/transparent" 
     android:src="@drawable/ic_action_camera" /> 

</RelativeLayout> 

+0

У вашего макета описания есть атрибут layout_margin, уже указанный в xml? – Ogen

+0

Да, я должен удалить его? – AbsoluteHeero

+0

см. Мой ответ – Ogen

ответ

1

Вы уже получили атрибут layout_margin уже указанный в XML так попробовать это вместо этого. Получите атрибут из XML и измените его, а не создайте новые параметры.

Попробуйте это:

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) description 
     .getLayoutParams(); 

params.setMargins(8, 8, 8, 8); 
+0

Извините, что не работает – AbsoluteHeero

2
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

    params1.setMargins(8, 8, 8, 8); 

    description.setLayoutParams(params1); 

попробовать это. поскольку он сейчас работает и обеспечит рентабельность.

Посмотрите мои обновленные ан. из xml макет поля хорошо работает, вы можете установить маржу из xml. как

<TextView 
    android:id="@+id/textViewTip" 
    android:textSize="12sp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/imageViewItem" 
    android:layout_alignTop="@+id/imageViewItem" 
    android:layout_marginLeft="8dp" 
    android:layout_marginRight="20dp" 
    android:layout_marginTop="22dp" 
    android:layout_marginBottom="22dp" 
    android:layout_toLeftOf="@+id/imageButtonCamera" 
    android:layout_toRightOf="@+id/imageViewItem" 
    android:text="Changable" /> 

он работает, и если вы хотите программно изменить поля, используйте следующий код.

LayoutParams p=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
    TextView tv=(TextView)findViewById(R.id.textViewTip); 

    p.setMargins(30, 30, 30, 30); 
    tv.setLayoutParams(p); 
+0

То, что я сделал первым – AbsoluteHeero

+0

, если вы уже указали атрибуты в xml, тогда вы должны получить эти атрибуты и изменить их, а затем установить параметры макета на макет описания – Awais

+0

Я не могу установить оба параметра в мой текст. Если я установлю marginlayoutparams, я собираюсь потерять свои относительные параметры. – AbsoluteHeero

Смежные вопросы