zoukankan      html  css  js  c++  java
  • 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属性的设置

  • 相关阅读:
    Java数据类型转换(自动转换和强制转换)
    Java数据类型以及变量的定义
    Java8新特性_日期时间新类 LocalDate、LocalTime、LocalDateTime
    Java8新特性_接口中的默认方法
    java8 新特性 Optional容器类
    Java8新特性 并行流与串行流 Fork Join
    Java8新特性_stream API 练习
    IDEA导入JUnit4
    Reduce:规约;Collector:收集、判断性终止函数、组函数、分组、分区
    【Linux】排序命令sort
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_python_156155.html
Copyright © 2011-2022 走看看