Android的RelativeLayout的编程设置centerInParent [英] Android RelativeLayout programmatically set centerInParent

查看:378
本文介绍了Android的RelativeLayout的编程设置centerInParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个RelativeLayout的:

I have a relativelayout like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                              android:orientation="horizontal"
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:layout_marginTop="10dip">

                            <Button android:id="@+id/negativeButton"
                                    android:layout_width="wrap_content" 
                                    android:layout_height="wrap_content"
                                    android:textSize="20dip"
                                    android:textColor="#ffffff"
                                    android:layout_alignParentLeft="true"
                                    android:background="@drawable/black_menu_button"
                                    android:layout_marginLeft="5dip"
                                    android:layout_centerVertical="true"
                                    android:layout_centerHorizontal="true"/> 

                            <Button android:id="@+id/positiveButton"
                                    android:layout_width="wrap_content" 
                                    android:layout_height="wrap_content"
                                    android:textSize="20dip"
                                    android:textColor="#ffffff"
                                    android:layout_alignParentRight="true"
                                    android:background="@drawable/blue_menu_button"
                                    android:layout_marginRight="5dip"
                                    android:layout_centerVertical="true"
                                    android:layout_centerHorizontal="true"/>
          </RelativeLayout>

我希望能够以编程为positiveButton设置相同的效果:

I want to be able to programatically set for positiveButton the same effect as:

android:layout_centerInParent="true"

我怎样才能使这个编程?

How can I make this programatically ?

推荐答案

经过充分测试,但这样的的工作:

Completely untested, but this should work:

View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams = 
    (RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);

这篇关于Android的RelativeLayout的编程设置centerInParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆