动态和以编程方式创建布局
Dynamically and programmatically create the layout
我正在尝试以编程方式创建一个布局,其中有一个 ImageView
带有两个按钮。现在 ImageView
将位于中心,按钮将位于布局的底部。但我做不到。这是我的代码
public class StackView extends RelativeLayout {
....................
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.LEFT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.RIGHT);
addView(imageview, 0, params);
Button button=new Button(context);
button.setText("left");
button.setGravity(Gravity.START);
addView(button, 1, params1);
Button button1=new Button(context);
button1.setText("right");
button.setGravity(Gravity.END);
addView(button1, 2,params2);
}
我知道了answer.Here我是怎么做的。
Button button=new Button(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(BELOW,card.getId());
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// params2.addRule(BELOW,card.getId());
params2.addRule(ALIGN_PARENT_RIGHT,button.getId());
addView(imageview, 0, params);
button.setText("left");
button.setGravity(Gravity.START);
addView(button, 1, params1);
Button button1=new Button(context);
button1.setText("right");
//button.setGravity(Gravity.END);
addView(button1, 2,params2);
试试这个
activity_main.xml
<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"
tools:context="com.example.demo.MainActivity"
android:id="@+id/layout" >
</RelativeLayout>
public class MainActivity extends Activity {
private RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
//for imageview
RelativeLayout.LayoutParams paramBtn = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramBtn.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView imgView = new ImageView(getApplicationContext());
imgView.setId(1);
imgView.setBackgroundResource(R.drawable.ic_launcher);
imgView.setLayoutParams(paramBtn);
layout.addView(imgView);
RelativeLayout.LayoutParams param1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
param1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Button btn1 = new Button(getApplicationContext());
btn1.setId(2);
btn1.setText("Submit");
btn1.setLayoutParams(param1);
layout.addView(btn1);
RelativeLayout.LayoutParams param2 = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param2.addRule(RelativeLayout.RIGHT_OF,btn1.getId());
param2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Button btn2 = new Button(getApplicationContext());
btn2.setId(3);
btn2.setText("Submit-2");
btn2.setLayoutParams(param2);
layout.addView(btn2);
}
}
我正在尝试以编程方式创建一个布局,其中有一个 ImageView
带有两个按钮。现在 ImageView
将位于中心,按钮将位于布局的底部。但我做不到。这是我的代码
public class StackView extends RelativeLayout {
....................
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.LEFT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM|Gravity.RIGHT);
addView(imageview, 0, params);
Button button=new Button(context);
button.setText("left");
button.setGravity(Gravity.START);
addView(button, 1, params1);
Button button1=new Button(context);
button1.setText("right");
button.setGravity(Gravity.END);
addView(button1, 2,params2);
}
我知道了answer.Here我是怎么做的。
Button button=new Button(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(BELOW,card.getId());
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// params2.addRule(BELOW,card.getId());
params2.addRule(ALIGN_PARENT_RIGHT,button.getId());
addView(imageview, 0, params);
button.setText("left");
button.setGravity(Gravity.START);
addView(button, 1, params1);
Button button1=new Button(context);
button1.setText("right");
//button.setGravity(Gravity.END);
addView(button1, 2,params2);
试试这个
activity_main.xml
<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"
tools:context="com.example.demo.MainActivity"
android:id="@+id/layout" >
</RelativeLayout>
public class MainActivity extends Activity {
private RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.layout);
//for imageview
RelativeLayout.LayoutParams paramBtn = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
paramBtn.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView imgView = new ImageView(getApplicationContext());
imgView.setId(1);
imgView.setBackgroundResource(R.drawable.ic_launcher);
imgView.setLayoutParams(paramBtn);
layout.addView(imgView);
RelativeLayout.LayoutParams param1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
param1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Button btn1 = new Button(getApplicationContext());
btn1.setId(2);
btn1.setText("Submit");
btn1.setLayoutParams(param1);
layout.addView(btn1);
RelativeLayout.LayoutParams param2 = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param2.addRule(RelativeLayout.RIGHT_OF,btn1.getId());
param2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Button btn2 = new Button(getApplicationContext());
btn2.setId(3);
btn2.setText("Submit-2");
btn2.setLayoutParams(param2);
layout.addView(btn2);
}
}