问题描述
我看过几个示例,这些示例用于通过程序(来自Java)为android中的视图设置相对位置.但是在我的特定情况下,我有2个按钮(不是视图),分别说"button_tag"和"button_rate",它们是通过xml制作的,并且默认情况下在"button_rate"上方排列为"button_tag".如果在任何时间点上都有一种机制,我可以通过该机制动态地使"button_rate"下面的"button_tag".
I had seen a couple of examples for setting the relative position through program (from java) for views in android. But in my particular case i have 2 buttons (which are not views) say "button_tag" and "button_rate" which are made via xml and are arranged such as "button_tag" above "button_rate" by default. If at any point of time is there a mechanism by which i can make "button_tag" below "button_rate" dynamically.
<Button
android:id="@+id/button_tag"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="1dp"
android:background="@drawable/img_whitebackground"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:text="@string/string_tag"
android:onClick="click_addscreen" >
</Button>
<Button
android:id="@+id/button_rate"
android:layout_marginTop="1dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/string_rate"
android:gravity="left|center_vertical"
android:paddingLeft="5dip"
android:onClick="click_addscreen"
android:background="@drawable/img_whitebackground" >
</Button>
两个按钮的xml部分如图所示
The xml part of the two buttons are as shown
推荐答案
您可以添加规则以动态设置位置.
You can add rules to set position dynamically.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
p.addRule(RelativeLayout.BELOW, R.id.button_rate);
buttonTag.setLayoutParams(p);
这篇关于动态更改一个按钮相对于另一个按钮的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!