I have a main RelativeLayout object and I want add a LinearLayout object into this,but also by setting the LayoutParams of LinearLayout,the elements inside it,align to left,while I would like that the last element is attached at the right end of the window.
This my first test code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
TextView text = new TextView(this);
text.setText("Something");
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}
}
and this is the result:
if I edit the code in this way:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar= getActionBar();
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
attach.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView text = new TextView(this);
text.setText("Something");
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}}
I get this:
as you can see the attacch button disappears...
In short,I want center text and align attach button to the right
// try this way
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
LinearLayout Bottom = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
Button scale = new Button(this);
LinearLayout.LayoutParams scaleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
scaleParams.gravity=Gravity.CENTER;
scale.setText("Scale");
Button attach = new Button(this);
LinearLayout.LayoutParams attachParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
attachParams.gravity=Gravity.CENTER;
attach.setText("Attacch");
TextView text = new TextView(this);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textParams.gravity=Gravity.CENTER;
text.setText("Something");
Bottom.addView(scale,scaleParams);
Bottom.addView(text,textParams);
Bottom.addView(attach,attachParams);
mainLayout.addView(Bottom,layoutParams);
setContentView(mainLayout,new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.