Android - 以编程方式定位视图 [英] Android - positioning views programmatically

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

问题描述

我想弄清楚如何在 android 中以编程方式定位视图.例如,我们有这个 XML 代码.

I want to figure out how to position views programmatically in android. Lets say for example we have this XML code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:id="@+id/mainLayout">
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="121dp"
    android:layout_marginTop="140dp"
    android:text="Results" /></RelativeLayout>

如何在 android 中以编程方式实现此布局?因为我想要我的文本视图的随机位置.

How can I achieve this layout programmatically in android? because I want to have a random position of my textview.

推荐答案

check this..

check this..

    RelativeLayout main = new RelativeLayout(this);
    main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));



    TextView textV = new TextView(this);
        textV.setGravity(Gravity.LEFT);
    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(121, 140, 0, 0);
    textV.setLayoutParams(layoutParams);
    text.setText("Result ");

main .addView(text);

这篇关于Android - 以编程方式定位视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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