以编程方式更改relativelayout位置及其子视图

时间:2014-05-20 07:03:07

标签: android android-layout relativelayout

如何以编程方式更改布局ID:rl_generic相对布局的位置? 目前它居中。我希望它的位置能够改变到左上角10秒钟。接下来10秒的右上角及其子视图。

 <RelativeLayout 
android:id="@+id/ClockScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"       
    android:layout_centerInParent="true"
    android:id="@+id/rl_generic"
    android:visibility="visible" >

    <TextView
        android:id="@+id/TimeDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="12:00"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="100sp" />

    <TextView
        android:id="@+id/textViewDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TimeDisplay"
        android:layout_centerHorizontal="true"
        android:paddingTop="1dp"
        android:text="" />
   </RelativeLayout>
   </RelativeLayout>

1 个答案:

答案 0 :(得分:1)

使用以下代码更改布局的位置。

public class SampleActivity extends Activity {

private RelativeLayout miView;
private RelativeLayout.LayoutParams params;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listview);
    miView = (RelativeLayout) findViewById(R.id.rl_generic);
    params=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Log.d("Sample Acitivity::", "Inside Run MTHD:::");
            miView.setLayoutParams(params);
        }
    }, 5000);
   }
}

让我知道您的反馈意见..

相关问题