android 布局问题:只有在软键盘出现后,才将布局高度获取到match_parent

brgchamk  于 2023-05-21  发布在  Android
关注(0)|答案(1)|浏览(122)

我试图创建一个单一的活动应用程序,使用“EditText”收集数据,并将其保存到Firebase实时数据库。我已经完成了这个项目所需要的一切。但是在设置了所有内容并获得一个成功的构建之后,一个在活动文件中有一些更改的新构建给了我以下布局问题。当软键盘存在时,可以很好地显示每个小部件。如果我关闭键盘,**它会缩小到顶部。**您可以看到以下截图以供参考。谢谢你帮我我使用的代码如下。
1.无键盘

1.键盘存在时

3.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/parent_group">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="।। श्री गणेशाय नम:"
    android:textSize="22sp"
    android:gravity="center"
    android:textColor="@android:color/holo_orange_dark"
    android:padding="8dp"
    android:layout_marginTop="8dp"
    android:id="@+id/start_txt"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbname"
    android:hint="वर / वधु का नाम "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/start_txt"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbpname"
    android:hint="पिता का नाम "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbname" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/genderrg"
                android:orientation="horizontal"
                android:layout_below="@+id/gbpname">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbmale"
                android:text="वर"
                android:layout_marginStart="10dp"
                android:textSize="22sp"
                android:textColor="@android:color/holo_orange_dark"
                android:layout_weight="1"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/rbfmale"
                android:text="वधु"
                android:layout_marginEnd="10dp"
                android:textSize="22sp"
                android:textColor="@android:color/holo_orange_dark"
                android:layout_weight="1"/>

            </RadioGroup>



<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbgotra"
    android:hint="गोत्र "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/genderrg"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbdob"
    android:hint="जन्‍म तिथि DD-MM-YYYY "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbgotra"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbheight"
    android:hint="ऊंचाई"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbdob"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbcolor"
    android:hint="रंग"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbheight"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbedu"
    android:hint="शिक्षा "
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbcolor"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbjob"
    android:hint="व्‍यवसाय"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbedu"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbhobby"
    android:hint="रूचि के विषय"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbjob"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gbmail"
            android:hint="ई-मेल"
            android:textSize="22sp"
            android:padding="8dp"
            android:layout_marginStart="8dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textColorHint="@android:color/holo_orange_dark"
            android:layout_below="@+id/gbhobby"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/gbphone"
            android:hint="मोबाईल नंबर"
            android:textSize="22sp"
            android:padding="8dp"
            android:layout_marginStart="8dp"
            android:textColor="@android:color/holo_orange_dark"
            android:textColorHint="@android:color/holo_orange_dark"
            android:layout_below="@+id/gbmail"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/gbaddr"
    android:hint="स्‍थायी पता"
    android:textSize="22sp"
    android:padding="8dp"
    android:layout_marginStart="8dp"
    android:textColor="@android:color/holo_orange_dark"
    android:textColorHint="@android:color/holo_orange_dark"
    android:layout_below="@+id/gbphone"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/add_details"
            android:text="जानकारी जोडें"
            android:layout_below="@id/gbaddr"
            android:layout_margin="8dp"
            android:textSize="22sp"
            android:backgroundTint="@android:color/holo_orange_dark"/>

4.MainActivity.java

package com.nilesh.rbndj;

import android.os.Bundle;
import android.os.Handler;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.core.view.WindowCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.nilesh.rbndj.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {



    private static final String TAG = "MainActivity";
    private AdView mAdView;

    EditText name,fname,gotra,dob,hght,colorr,edu,job,hobby,mail,phone,addr;
    Button addButton;
    RadioButton male,fmale;

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

//        mAdView = findViewById(R.id.adView);
//        AdRequest adRequest = new AdRequest.Builder().build();
//        mAdView.loadAd(adRequest);

        name = findViewById(R.id.gbname);
        fname = findViewById(R.id.gbpname);
        gotra = findViewById(R.id.gbgotra);
        dob = findViewById(R.id.gbdob);
        hght = findViewById(R.id.gbheight);
        colorr = findViewById(R.id.gbcolor);
        edu = findViewById(R.id.gbedu);
        job = findViewById(R.id.gbjob);
        hobby = findViewById(R.id.gbhobby);
        mail = findViewById(R.id.gbmail);
        phone = findViewById(R.id.gbphone);
        addr = findViewById(R.id.gbaddr);
        addButton = findViewById(R.id.add_details);
        male = findViewById(R.id.rbmale);
        fmale = findViewById(R.id.rbfmale);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Members members = new Members();
                DatabaseReference mDatabase;

                mDatabase = FirebaseDatabase.getInstance().getReference();

                String id, namee, fnamee, gotraa, dobb, hghtt, colorrr, eduu, jobbb, hobbyy, email, phonee, addrr, rbm, rbf;

                namee = name.getText().toString();
                fnamee = fname.getText().toString();
                gotraa = gotra.getText().toString();
                dobb = dob.getText().toString();
                hghtt = hght.getText().toString();
                colorrr = colorr.getText().toString();
                eduu = edu.getText().toString();
                jobbb = job.getText().toString();
                hobbyy = hobby.getText().toString();
                email = mail.getText().toString();
                phonee = phone.getText().toString();
                addrr = addr.getText().toString();
                rbm = male.getText().toString();
                rbf = fmale.getText().toString();

                id = phonee;

                mDatabase.child("biodata").child(id).child("name").setValue(namee);
                mDatabase.child("biodata").child(id).child("father").setValue(fnamee);
                mDatabase.child("biodata").child(id).child("gotra").setValue(gotraa);
                mDatabase.child("biodata").child(id).child("dob").setValue(dobb);
                mDatabase.child("biodata").child(id).child("height").setValue(hghtt);
                mDatabase.child("biodata").child(id).child("color").setValue(colorrr);
                mDatabase.child("biodata").child(id).child("education").setValue(eduu);
                mDatabase.child("biodata").child(id).child("job").setValue(jobbb);
                mDatabase.child("biodata").child(id).child("hobby").setValue(hobbyy);
                mDatabase.child("biodata").child(id).child("email").setValue(email);
                mDatabase.child("biodata").child(id).child("phone").setValue(phonee);
                mDatabase.child("biodata").child(id).child("address").setValue(addrr);

                if (male.isChecked()) {
                    members.setGender(rbm);
                    mDatabase.child("biodata").child(id).child("gender").setValue(rbm);

                } else {
                    members.setGender(rbf);
                    mDatabase.child("biodata").child(id).child("gender").setValue(rbf);
                }

                mDatabase.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
//                        if (snapshot.hasChild("message")){
//                            clearForm();
//                        }
                    }

                    @Override
                    public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                    }

                    @Override
                    public void onChildRemoved(@NonNull DataSnapshot snapshot) {

                    }

                    @Override
                    public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {

                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });

            }
        });



    }

//    public  void  clearForm() {
//
//        ViewGroup group = (ViewGroup) findViewById(R.id.parent_group);
//        for (int i = 0, count = group.getChildCount(); i < count; ++i) {
//            View view = group.getChildAt(i);
//            if (view instanceof EditText) {
//                ((EditText) view).setText(null);
//            }
//        }

}

我希望这些信息足以帮助你解决我的问题。

5f0d552i

5f0d552i1#

问题是RadioGroup高度。在您的代码中,RadioGroup的高度是match_parent,因此将xml文件中的RadioGroup替换为android:layout_height="wrap_content"

相关问题