Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用java代码编写此xml_Java_Android_Xml - Fatal编程技术网

如何用java代码编写此xml

如何用java代码编写此xml,java,android,xml,Java,Android,Xml,Android XML: <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:layout_height="28dp" android:layout_width="180dp" android:layout_toRightOf="@+id/signinemailtxt" android:paddin

Android XML:

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

/>
如何获取版面边距的java代码中选项的版面边距、版面边距和版面边距:

  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);
LayoutParams params = new LayoutParams(context, attrs);
params.setMargins(0, 5, 0, 5); //setMargins (int left, int top, int right, int bottom)

Txt.setLayoutParams(params);

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

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

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

如果我希望此文本视图处于特殊位置。。如果你想要x=34和y=30像素,那么x=34px和y=30px。只需更改此行:RelativeLayout.LayoutParams lp=新的RelativeLayout.LayoutParams(34,30);我希望textview位于34,30中,而不是我在此处放置的整个布局上下文,attrsContext context=getApplicationContext();//您可以使用YourClass.this tooXmlPullParser parser=res.getXml(R.layout.your layout);AttributeSet attributes=Xml.asAttributeSet(解析器);其中Resources res=getResources();XmlPullParser=res.getXml(R.layout.main);AttributeSet attributes=Xml.asAttributeSet(解析器);Context=getApplicationContext();LayoutParams params=新的LayoutParams(上下文、属性);这是一个错误
RelativeLayout layout = (RelativeLayout) findViewById(R.id.LayoutId);
TextView Txt = (TextView) findViewById(R.id.TextViewId);