类之间的对齐布局交换 - Android Studio

时间:2015-02-17 23:38:15

标签: android android-layout layout android-studio android-relativelayout

我正在实施一个使用数据库的程序,并与不同的layouts条目和编辑用户进行互动。

我正在所有屏幕上使用RelativeLayout。在其中一个layouts中,我插入一个完美对齐的按钮,然后根据请求提供命令 android:visibility="gone"

问题在于,当我需要在命令 editarBt.setVisibility(View.VISIBLE)使用它时,按钮会显示不对齐并与字段重叠以输入信息。< / p>

有没有办法通过按钮保持命令的位置

我不会把整个代码放在一起,因为它有7个类,所以我只是把它们放在那些感兴趣的类中。

EnterPatientActivity Class

package br.luizhmu.aulas_android_sqlite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* Created by LuizHMU on 2/17/15.
*/
public class EnterPatientActivity extends Activity {

private Paciente paciente = new Paciente();
private EditText nomeEt;
private EditText emailEt;
private EditText senhaEt;
private Button salvarBt;
private Button editarBt;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inserir_paciente);

    nomeEt = (EditText) findViewById(R.id.editTextNome);
    emailEt = (EditText) findViewById(R.id.editTextEmail);
    senhaEt = (EditText) findViewById(R.id.editTextSenha);
    salvarBt = (Button) findViewById(R.id.buttonSalvar);
    editarBt = (Button) findViewById(R.id.buttonEditar);

    Intent intent = getIntent();
    if(intent != null){
        Bundle bundle = intent.getExtras();
        if(bundle != null){

            paciente.setId(bundle.getLong("id"));
            paciente.setNome(bundle.getString("nome"));
            paciente.setEmail(bundle.getString("email"));

            nomeEt.setText(paciente.getNome());
            emailEt.setText(paciente.getEmail());

            senhaEt.setVisibility(View.GONE);
            salvarBt.setVisibility(View.GONE);
            editarBt.setVisibility(View.VISIBLE);

        }
    }
}

public void salvar(View view){
    paciente.setNome(nomeEt.getText().toString());
    paciente.setEmail(emailEt.getText().toString());
    paciente.setSenha(senhaEt.getText().toString());

    DataBase bd = new DataBase(this);
    bd.inserir(paciente);

    Toast.makeText(this, "Paciente inserido com sucesso!", Toast.LENGTH_SHORT).show();
}


public void editar(View view){
    paciente.setNome(nomeEt.getText().toString());
    paciente.setEmail(emailEt.getText().toString());

    DataBase bd = new DataBase(this);
    bd.atualizar(paciente);

    Toast.makeText(this, "Paciente \""+paciente.getNome()+"\" atualizado com sucesso.", Toast.LENGTH_SHORT).show();
}

}

activity_inserir_paciente.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#ffffea0a"
tools:context=".EnterPatientActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Novo paciente"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ff1727ff"
    android:textSize="20dp"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="*Nome"
    android:ems="10"
    android:id="@+id/editTextNome"
    android:layout_below="@+id/textView3"
    android:layout_alignRight="@+id/buttonSalvar"
    android:layout_alignEnd="@+id/buttonSalvar" />

<EditText
    android:hint="Telefone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:ems="10"
    android:id="@+id/editTextTelefone"
    android:layout_below="@+id/editTextNome"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<EditText
    android:hint="*E-mail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/editTextEmail"
    android:layout_below="@+id/editTextTelefone"
    android:layout_alignLeft="@+id/editTextTelefone"
    android:layout_alignStart="@+id/editTextTelefone" />

<EditText
    android:hint="*Senha"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPassword"
    android:ems="10"
    android:id="@+id/editTextSenha"
    android:layout_below="@+id/editTextEmail"
    android:layout_alignLeft="@+id/editTextEmail"
    android:layout_alignStart="@+id/editTextEmail" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Salvar"
    android:id="@+id/buttonSalvar"
    android:onClick="salvar"
    android:layout_below="@+id/textView4"
    android:layout_alignRight="@+id/editTextSenha"
    android:layout_alignEnd="@+id/editTextSenha" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Editar"
    android:id="@+id/buttonEditar"
    android:layout_alignTop="@+id/buttonSalvar"
    android:layout_toLeftOf="@+id/buttonSalvar"
    android:layout_toStartOf="@+id/buttonSalvar"
    android:visibility="gone"
    android:onClick="editar"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="* Campos de preenchimento obrigatório"
    android:textColor="#000000"
    android:id="@+id/textView4"
    android:layout_below="@+id/editTextSenha"
    android:layout_alignLeft="@+id/editTextSenha"
    android:layout_alignStart="@+id/editTextSenha" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

RelativeLayout中,您可以使用 layout_toLeftOf layout_toRightOf layout_above 来阻止视图重叠layout_below 属性,它们将视图ID视为值。

此外,您可能希望使用View.INVISIBLE而不是View.GONE:前者会在布局期间考虑视图,但会隐藏它。后者会假装视图不存在,因此会改变你的布局结果。