Когда я нажимаю кнопку «Добавить», отображается представление и должно быть непосредственно под кнопкой добавления. Однако, когда я нажимаю кнопку, между кнопкой добавления и добавленным видом есть пробел. Я попытался установить гравитацию в верхний слой в файле xml и java. Когда я снова нажимаю кнопку «Добавить» и получаю следующий вид, первое представление оно подталкивает туда, где оно должно быть. Что я делаю не так?Устранение неполадок при правильной настройке
public class MainActivity extends Activity {
public int count = 1;
int layoutUse[] = {0,0,0,0,0};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addTroopBtn = (Button) findViewById(R.id.addTroop);
addTroopBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(count <= 2)
{
//inputName(v);
inputName(v);
}
else
{
}
}
});
Button removeTroop1 = (Button)findViewById(R.id.remove1);
removeTroop1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeTroop1();
}
});
Button removeTroop2 = (Button)findViewById(R.id.remove2);
removeTroop2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
removeTroop2();
}
});
}
public void inputName(final View v){
AlertDialog.Builder alert = new AlertDialog.Builder(this);
//alert.setTitle("Enter Name and Rank");
alert.setMessage("Please Enter A Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable name = input.getText();
addTroop(name);
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
public void addTroop(Editable name){
if(layoutUse[0] == 0 && count == 1)
{
LinearLayout mainView = (LinearLayout)findViewById(R.id.manageTroopsMain);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.trpLayout1);
Button troop1 = (Button)findViewById(R.id.troop1);
troop1.setText("Button 1");
mainView.removeView(layout1);
mainView.addView(layout1);
layout1.setVisibility(View.VISIBLE);
layoutUse[0]++;
count++;
}
else if(layoutUse[1] == 0 && count == 2)
{
LinearLayout mainView = (LinearLayout)findViewById(R.id.manageTroopsMain);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.trpLayout2);
Button troop2 = (Button)findViewById(R.id.troop2);
troop2.setText("Button 2");
mainView.removeView(layout2);
mainView.addView(layout2);
layout2.setVisibility(View.VISIBLE);
layoutUse[1]++;
count++;
}
}
public void removeTroop1(){
LinearLayout layout = (LinearLayout)findViewById(R.id.trpLayout1);
layout.setVisibility(View.GONE);
layoutUse[0]--;
count--;
}
public void removeTroop2(){
LinearLayout layout = (LinearLayout)findViewById(R.id.trpLayout2);
layout.setVisibility(View.GONE);
layoutUse[1]--;
count--;
}
}