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属性的设置

  • 相关阅读:
    第三次实验总结
    实验总结
    自我介绍
    BGP学习笔记
    source insight用于C语言编程的工具脚本
    LevelDB源码剖析
    LevelDB源码之五Current文件Manifest文件版本信息
    LevelDB源码之六缓存机制
    LevelDB源码之四LOG文件
    jQuery向动态生成的内容添加事件响应
  • 原文地址:https://www.cnblogs.com/zhangshuli-1989/p/zhangshuli_python_156155.html
Copyright © 2011-2022 走看看