Android布局编程
layout\layout\u main.xmlAndroid布局编程,android,layout,programmatically-created,Android,Layout,Programmatically Created,layout\layout\u main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rR1MessageBubble" android:layout_width="match_parent" android:layout_heig
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rR1MessageBubble"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rR2MessageBubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/drawable_sky_background_frame">
</RelativeLayout>
</RelativeLayout>
输出
要求
我只想在layout_main.xml中添加layout_component.xml的多个实例,因此请不要使用否定投票,而是通过建议我实现这一点的方法…谢谢,可能是因为您将文本设置为“World”
您犯了一个小错误,伙计,您将错误的
LayoutParam
分配给view2
取代
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();
与
希望您理解您的错误。\@markieo您的意思是说view1和view2指向同一引用??为什么投反对票…我只想在layout\u main.xml中添加layout\u component.xml的多个实例,所以请不要投反对票,而是建议我通过以下途径实现此目的…谢谢@非常感谢您…取而代之对于反对票,您提出了解决方案:)@@1抱歉,再次在下面打扰您,其中一个有相同的问题params.addRule(RelativeLayout.down,view1.getId());视图1.setId(8);先把我也赶走:)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#FF07DD43" />
<corners android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
</shape>
![package com.kore.layoutdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* @author ayadav
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
RelativeLayout rR2MessageBubble = (RelativeLayout)findViewById(R.id.rR2MessageBubble);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();
//View-1
View view1 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);
rR2MessageBubble.addView(view1);
//View-2
View view2 = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.layout_component, null);
TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
txtViewTest.setText("World");
//params.addRule(RelativeLayout.RIGHT_OF, view1.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
view2.setLayoutParams(params);
rR2MessageBubble.addView(view2);
}
}
TextView txtViewTest = (TextView) view2.findViewById(R.id.txtViewTest);
txtViewTest.setText("World");
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) rR2MessageBubble.getLayoutParams();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100); //or whatever width, height as per your need.