动态设置控件高度、位置

有布局大致如下:  
 <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:orientation="vertical" >  
  
        <FrameLayout  
            android:id="@+id/framelayout"  
            android:layout_width="match_parent"  
            android:layout_height="200dip" >  
  
           ---------  
       ---------  
             
        </FrameLayout>  
 </LinearLayout>  
  
  
  
现要在代码中设置framelayout的高度:    
    
//LinearLayout 为父容器布局    
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,  
                                        LinearLayout.LayoutParams.MATCH_PARENT);    
framelayout.setLayoutParams(p); //设置framelayout宽高(MATCH_PARENT,MATCH_PARENT)

//设置tv位置(垂直居中、居右,距离右边15px)  
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,  
                    RelativeLayout.LayoutParams.MATCH_PARENT);  
tv.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);  //tv的父容器为RelativeLayout
rl.rightMargin = ScreenUtils.dipConvertPx(mContext, 15);  
tv.setLayoutParams(rl);


/** 
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
     * @param mContext 
     * @param dpValue 
     */  
    public static int dipConvertPx(Context mContext, float dpValue) {  
        try {  
            float scale = mContext.getResources().getDisplayMetrics().density;  
            return (int) (dpValue * scale + 0.5f);  
        } catch (Exception e) {  
        }  
        return -1;  
    }

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include
            android:id="@+id/ahm_top_relative"
            layout="@layout/include_homemain_top" />

        <ViewPager
            android:id="@+id/ahm_viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/ahm_top_relative"
            android:scrollbars="none" />

        <RadioGroup
            android:id="@+id/ahm_radiogroup"
            android:layout_width="match_parent"
            android:layout_height="55.0dip"
            android:layout_alignParentBottom="true"
            android:background="@color/bg_E6ffffff"
            android:orientation="horizontal"
            android:paddingBottom="2dp"
            android:paddingTop="2dp">

            <RadioButton
                android:id="@+id/ahm_message_rb"
                ----
		---- />

            <RadioButton
                android:id="@+id/ahm_index_rb"
                -----
		-----/>

            <RadioButton
                android:id="@+id/ahm_wo_rb"
                -----
		-----/>
        </RadioGroup>


    </RelativeLayout>



现要在代码中设置 viewpager位于ahm_radiogroup之上(android:layout_above="@+id/ahm_radiogroup")
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.MATCH_PARENT);
rl.addRule(RelativeLayout.ABOVE, R.id.ahm_radiogroup);
ahm_viewpager.setLayoutParams(rl);




现要在代码中设置 viewpager不位于ahm_radiogroup之上(还原到布局xml)
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
							RelativeLayout.LayoutParams.MATCH_PARENT);
rl.addRule(RelativeLayout.BELOW,R.id.ahm_top_relative);//viewpager位于ahm_top_relative之下
rl.addRule(RelativeLayout.ABOVE, 0);//清除刚才设置的(位于ahm_radiogroup之上)
am_viewpager.setLayoutParams(rla);

在Android中,可以使用LayoutParams动态设置控件位置,LayoutParams是ViewGroup.LayoutParams的子类,用于设置控件布局中的位置和大小。LayoutParams可以根据控件所在的父布局类型进行选择,例如RelativeLayout.LayoutParams、LinearLayout.LayoutParams等。 以下是通过LayoutParams动态设置控件位置的示例代码: ``` // 创建一个新的TextView TextView textView = new TextView(this); textView.setText("Hello World!"); // 创建一个RelativeLayout.LayoutParams对象,设置宽度和高度为WRAP_CONTENT RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ); // 设置TextView的左上角对齐父布局的左上角 layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT); // 设置TextView的位置 layoutParams.topMargin = 50; layoutParams.leftMargin = 50; // 将LayoutParams设置给TextView textView.setLayoutParams(layoutParams); // 将TextView添加到父布局中 RelativeLayout relativeLayout = findViewById(R.id.relativeLayout); relativeLayout.addView(textView); ``` 在上述代码中,首先创建了一个新的TextView,然后创建了一个RelativeLayout.LayoutParams对象,并设置了TextView的宽度和高度为WRAP_CONTENT。接着,通过addRule方法设置TextView的左上角对齐父布局的左上角,再通过topMargin和leftMargin设置TextView的位置。最后,将LayoutParams设置给TextView,并将TextView添加到父布局中。 总之,使用LayoutParams可以方便地动态设置控件位置和大小,开发者可以根据实际需求选择不同的LayoutParams类型,并根据需要设置控件位置和大小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值