TextView.getLayoutParams()是否具有所有layout_align属性?

时间:2013-11-20 19:27:47

标签: java android android-layout textview relativelayout

我有以下TextView

<RelativeLayout
            android:id="@+id/LayoutItem"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:background="@color/linharegistro_par"
            android:gravity="left"
            android:minHeight="65dp"
            android:padding="5dp"
            android:visibility="gone" >

        <TextView
            android:id="@+id/lblKg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/lblTroca"
            android:layout_alignBottom="@+id/lblTroca"
            android:layout_alignParentRight="true"
            android:background="@color/linharegistro_impar"
            android:paddingLeft="15dp"
            android:paddingTop="2dp"
            android:paddingBottom="2dp"
            android:paddingRight="10dp"
            android:gravity="left"
            android:textAlignment="gravity"
            android:text="Kg"
            android:textColor="@color/black" />
</RelativeLayout>

RelativeLayout内,正如您所见。

我正在生成这些元素的Clone,但要执行Clone,您需要设置所有元素属性。

以下是设置属性的Clone类:

public class Item {

    private RelativeLayout pnlItem;     
    private TextView lblKg;

    public Item(RelativeLayout pnlItem, TextView lblKg) {
        clone(pnlItem, lblIndice);
        this.pnlItem.addView(this.lblKg);                                       
    }

    public void clone(RelativeLayout pnlItem, TextView lblKg){

        this.pnlItem = new RelativeLayout(GruposForm.this);
        this.pnlItem.setLayoutParams(pnlItem.getLayoutParams());
        this.pnlItem.setBackgroundColor(getResources().getColor(R.color.linharegistro_impar));
        this.pnlItem.setVisibility(View.VISIBLE);
        this.pnlItem.setMinimumHeight(65);
        this.pnlItem.setPadding(pnlItem.getPaddingLeft(), pnlItem.getPaddingTop(), pnlItem.getPaddingRight(), pnlItem.getPaddingBottom());

        this.lblKg = new TextView(GruposForm.this);
        this.lblKg.setText(lblKg.getText()); 
         this.lblKg.setLayoutParams(lblKg.getLayoutParams());
        this.lblKg.setTextColor(lblKg.getTextColors());
        this.lblKg.setBackgroundColor(getResources().getColor(R.color.linharegistro_impar));
        this.lblKg.setPadding(lblKg.getPaddingLeft(), lblKg.getPaddingTop(), lblKg.getPaddingRight(), lblKg.getPaddingBottom());
        this.lblKg.setGravity(lblKg.getGravity());

    }

}

在我的代码中,我执行以下操作来克隆:

Item pnlItemClone = new Item(pnlItem, lblKg);
pnlContent.addView(pnlItemClone.pnlItem); //adding the cloned view to the main view.

pnlContent声明:

LinearLayout pnlContent = null;

pnlContent

的关联
pnlContent = (LinearLayout) findViewById(R.id.LayoutContent);

不要担心这个pnlContent,它只是一个LinearLayout,它包含Activity的所有内容,并且它正在运行。

我想知道this.lblKg.setLayoutParams(lblKg.getLayoutParams());是否设置并获取TextView的以下属性:

android:layout_alignBaseline="@+id/lblTroca"
android:layout_alignBottom="@+id/lblTroca"
android:layout_alignParentRight="true"

我问这是因为我发现生成的元素在View上放错了,我想解决这个问题。

this.lblKglblKg不同,this.lblKg是新克隆的TextViewlblKg是在{{1}开头声明的私有变量与Activity内的TextView相关联的是关联:。

XML Layout方法中:

onCreate

lblKg = (TextView) findViewById(R.id.lblKg);

的开头
Activity

任何帮助都将不胜感激。

0 个答案:

没有答案