今天需要在代码中动态的设置一个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
时间: 2024-10-10 02:22:41