Button 和 EditTextbox 以及布局问题
这是我的代码。因此,当单击按钮时,每次单击按钮时都会显示两个编辑文本框。他们来来往往,我希望他们并肩而行。 我还想知道我可以将线性布局更改为绝对布局,因为我希望按钮位置位于页面中间。另外,我希望当我单击按钮时,编辑文本框应该位于顶部而不是底部。 希望如此,我把这个问题说清楚了。
java代码:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
EditText a = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
addViewToRoot(a);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<Button android:layout_width="wrap_content" android:text="+" android:id="@+id/button" android:layout_height="wrap_content"></Button>
</LinearLayout>
This is my code. So when button is clicked, I have two edit text boxes that show up everytime, Button is clicked. They are coming up and down, instead I want them to be side by side.
Also I wanted to know that can I change the linear layout to absolute, since I want the button position to be at the middle of the page. Also, I want when I click the button edit text boxes should go to the top not at the bottom.
Hope so, I made this questions clear.
java code :
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
EditText a = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
a.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
addViewToRoot(a);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayout">
<Button android:layout_width="wrap_content" android:text="+" android:id="@+id/button" android:layout_height="wrap_content"></Button>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使它们并排改变线性布局方向=“水平”,但对我来说,听起来你宁愿使用RelativeLayout。在遇到困难之前先查一下它并尝试使用它 http:// developer.android.com/resources/tutorials/views/hello-relativelayout.html。
To make them side by side change linearlayout orientation="horizontal" but to me it sounds like you'd rather play around with RelativeLayout. Look it up before you get stuck and try to play with it http://developer.android.com/resources/tutorials/views/hello-relativelayout.html.