有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓如何用java代码编写此xml

Android XML:

<TextView  
安卓:layout_width="fill_parent" 
安卓:layout_height="wrap_content" 
安卓:text="@string/hello"
安卓:layout_height="28dp" 安卓:layout_width="180dp"
安卓:layout_toRightOf="@+id/signinemailtxt"
安卓:paddingLeft="50dp"
安卓:layout_marginTop="65dp"
安卓:background="#ffffff" 

/>

爪哇:

layout=  new  RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView Txt=new TextView(this);
Txt.setText("Name");

如何在java代码中获得layout_marginTop、layout_toRightOf选项


共 (3) 个答案

  1. # 1 楼答案

    不要声明布局和文本视图的新实例,而是获取您在xml中创建的实例:

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
    TextView Txt = (TextView) findViewById(R.id.TextViewId);
    

    现在,您可以编辑屏幕上显示的布局和文本视图

  2. # 2 楼答案

    对于布局_边距:

    LayoutParams params = new LayoutParams(context, attrs);
    params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)
    
    Txt.setLayoutParams(params);
    
  3. # 3 楼答案

      RelativeLayout layout = new RelativeLayout(this);
    TextView tv1 = new TextView(this);
    tv1.setText("A");
    
    TextView tv2 = new TextView(this);
    tv2.setText("B");
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
    lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
    
    layout.addView(tv1);        
    layout.addView(tv2, lp);