我有RelativeLayout,并且在RelativeLayout中有一个textview,我从服务器获取位置,如ALIGN_PARENT_LEFT,ALIGN_PARENT_TOP,基于该值我想改变textview的位置......
我的xml文件是....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:id="@+id/relativelayout"
android:orientation="vertical" >
<TextView
android:id="@+id/share_text"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center"
android:background="@drawable/share_text" />
</RelativeLayout>
我从Set position of an EditText and TextView in a RelativeLayout programatically获得了知识,但却无法帮助我
任何人都可以帮助我动态地改变textview的位置......
答案 0 :(得分:5)
试试这个
RelativeLayout.LayoutParams params1 = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
yourTextview.setLayoutParams(params1);
答案 1 :(得分:2)
怎么样
((RelativeLayout.LayoutParams)yourView.getLayoutParams()).alignParentTop =
serverResponse.equals("ALIGN_PARENT_TOP");
当然,您必须对服务器响应进行一些解析等等......