我正在制作一款适用于Android的应用程序,类似于我和我女朋友的钱箱,我们可以在这里添加购物或购买物品的账单。
在MainActivity的xml文件中,我已经创建了三个按钮,用于将人员添加到钱箱和其他一些东西。每次我将新人添加到钱箱时,它都应该在新的LinearLayout中创建一个新的文本视图。但它会像图片一样移动我的整个UI。
有人知道如何解决这个问题吗?
这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/mylayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="@+id/showDiagram"
android:text="@string/showDiagram"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:layout_gravity="bottom"/>
<Button
android:id="@+id/addExpenditure"
android:text="@string/addExpenditure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:layout_gravity="bottom"/>
<Button
android:id="@+id/addResident"
android:text="@string/addResident"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:layout_gravity="bottom"/>
</LinearLayout>
这是我的MainActivity:
public class MainActivity extends Activity {
EditText residentName;
int numberOfRes = 0;
DataHandler db = new DataHandler(this);
boolean notShow;
Button add;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button showDiagram = (Button) findViewById(R.id.showDiagram);
final LinearLayout myLayout = (LinearLayout)findViewById(R.id.mylayout);
//Ausgabe hinzufügen
Button addExpenditure = (Button) findViewById(R.id.addExpenditure);
addExpenditure.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
//Erstellung eines neuen Mitbewohners für die Kasse
Button addResident = (Button) findViewById(R.id.addResident);
addResident.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = createDialog();
residentName = (EditText)dialog.findViewById(R.id.residentname);
add =(Button)dialog.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
name = String.valueOf(residentName.getText());
db.insert(name);
dialog.dismiss();
myLayout.addView(createResident(name));
Log.d("debug","mylayout.addview() aufgerufen");
}
});
dialog.show();
}
});
}
public Dialog createDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("Dialog");
return dialog;
}
public LinearLayout createResident(String name){
LinearLayout newRes = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams newParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT,0.5f);
newRes.setLayoutParams(newParams);
newRes.setOrientation(LinearLayout.VERTICAL);
newRes.setBackgroundColor(Color.BLUE);
TextView res = new TextView(MainActivity.this);
LinearLayout.LayoutParams newParamsRes = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,0.5f);
res.setLayoutParams(newParamsRes);
res.setText(name);
res.setTextSize(25);
res.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
newRes.addView(res);
return newRes;
}
}
销毁用户界面
答案 0 :(得分:0)
我选择了RelativeLayout,它有效!