RelativeLayout-代码中成员控件width height

今天需要在代码中动态的设置一个textview的width跟height属性,记录下来。

textview在xml中的布局如下

 <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/setup_fragment_padding_bottom_vfive"
        android:layout_marginLeft="@dimen/setup_fragment_padding_left"
        android:layout_marginRight="@dimen/setup_fragment_padding_right"
        android:background="@null"
        >

        <!-- Buttons below -->
            <!--
        In order to show these buttons above the IME keyboard, we need to special case the to
        padding to a smaller height.
            -->
        <TextView
            android:id="@+id/previous"
            style="@style/accountSetupButtonVfive"
            android:layout_width="159dp"
            android:layout_height="wrap_content"
            android:background="@drawable/email_btn_set"
            android:layout_centerVertical="true"
            android:text="@string/previous" />
        <TextView
            android:id="@+id/manual_setup"
            style="@style/accountSetupButtonVfive"
            android:layout_width="159dp"
            android:layout_height="wrap_content"
            android:background="@drawable/email_btn_set"
            android:layout_centerVertical="true"
            android:visibility="gone"
            android:text="@string/account_setup_basics_manual_setup_action" />

        <TextView
            android:id="@+id/next"
            style="@style/accountSetupButtonVfive"
            android:layout_width="159dp"
            android:layout_height="wrap_content"
            android:background="@drawable/email_btn_next"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="@string/next" />
    </RelativeLayout>

 

 

在代码中的更改如下

import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

        if (visibility == View.GONE) {
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
            mPreviousButton.setBackgroundResource(R.drawable.email_btn_previous_only);
            mPreviousButton.setLayoutParams(lp);
        }

注意,如果这个控件的layout是LinearLayout,那么你需要使用相应的LinearLayout.LayoutParams

如果你还想调节控件之间的距离,这时候仍然需要借助LinearLayout.LayoutParams

        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        layoutParams1.setMargins(-10, 0, 0, 5);

它就实现了margin属性的设置

posted @ 2015-06-01 15:56  小白屋  阅读(506)  评论(0编辑  收藏  举报