活动视图显示在片段上

时间:2019-02-11 18:36:35

标签: android xml android-layout android-fragments

我在活动上有按钮,该按钮显示在片段上,只有显示的按钮,我做了搜索,并找出其渲染问题,但答案无效对我来说,此处外观如何。
片段 Fragment 活动 enter image description here

发布代码是否有可能位于按钮的空白处
Fragment.java

public class Smsar extends Fragment {


public Smsar() {
    // Required empty public constructor
}

View root;
Button mSmsar,mFinder;

 @Override
 public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

    // Inflate the layout for this fragment
    root= inflater.inflate(R.layout.fragment_smsar, container, false);

    mSmsar=(Button)root.findViewById(R.id.smsar);
    mFinder=(Button)root.findViewById(R.id.finder);

    mSmsar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        getActivity().onBackPressed();
        }
    });
    mFinder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Open Discover.

        }
    });



    return root;
 }
}

片段XML

  <?xml version="1.0" encoding="utf-8"?>
  <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:background="@drawable/main_background_difference"
tools:context=".Smsar">

<!-- TODO: Update blank fragment layout -->

<Button
    android:id="@+id/smsar"
    android:layout_width="250dp"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="59dp"
    android:background="@drawable/btn_rounded"
    android:fontFamily="@font/cabin"
    android:text="Smsar ?"
    android:textAlignment="center"
    android:textAllCaps="false"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="18sp"
    android:typeface="normal" />

<Button
    android:id="@+id/finder"
    android:layout_width="204dp"
    android:layout_height="63dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="138dp"
    android:background="@drawable/btn_rounded"
    android:fontFamily="@font/cabin"
    android:text="Want Apartment ?"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</RelativeLayout> 

活动XML

  <?xml version="1.0" encoding="utf-8"?>
  <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:background="@drawable/background"
android:orientation="vertical"
tools:context=".MainActivity"
android:id="@+id/mainView"
>

<ImageView
    android:id="@+id/logo"
    android:layout_width="162dp"
    android:layout_height="196dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="13dp"
    android:adjustViewBounds="true"
    android:maxHeight="64dp"
    android:maxWidth="64dp"
    app:srcCompat="@drawable/logo" />


<Button
    android:id="@+id/logIn"
    android:layout_width="218dp"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/userName"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="31dp"
    android:layout_marginBottom="113dp"
    android:background="@color/buttons"
    android:clickable="true"
    android:fontFamily="@font/catamaran"
    android:onClick="logIN"
    android:text="Login"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textSize="22sp" />

<EditText
    android:id="@+id/userName"
    android:layout_width="277dp"
    android:layout_height="wrap_content"

    android:layout_centerInParent="true"
    android:ems="10"
    android:fontFamily="@font/catamaran"
    android:hint="UserName"
    android:inputType="textPersonName"
    android:text="" />

<EditText
    android:id="@+id/password"
    android:layout_width="277dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/userName"
    android:layout_centerHorizontal="true"
    android:fontFamily="@font/catamaran"
    android:hint="Password"
    android:inputType="textPassword" />


<TextView
    android:id="@+id/_new"
    android:layout_width="159dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="-35dp"
    android:layout_marginBottom="0dp"

    android:layout_toStartOf="@+id/sginUp"
    android:fontFamily="@font/catamaran"
    android:text="New ? Start Now By"
    android:textAlignment="center"
    android:textSize="18sp" />

<TextView
    android:id="@+id/sginUp"
    android:layout_width="81dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="17dp"
    android:layout_marginBottom="0dp"
    android:layout_centerHorizontal="true"
    android:layout_toEndOf="@id/mError"
    android:clickable="true"
    android:fontFamily="@font/catamaran"
    android:text="SginUp"
    android:textAlignment="center"
    android:textColor="@color/links"
    android:textSize="18sp" />

<TextView
    android:id="@+id/mError"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="82dp"
    android:textColor="@android:color/holo_red_dark"
    android:visibility="invisible"
    android:text="mError" />
</RelativeLayout>

Activity.Java

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

    _login =(Button)findViewById(R.id.logIn);



    pref = getSharedPreferences("user_details", MODE_PRIVATE);
        if(pref.contains("username")&&pref.contains("password"))
             success();





    FragmentManager fm=getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.addToBackStack(null);
    ft.replace(R.id.mainView, mFrag);
    ft.commit();

    imageView=(ImageView)findViewById(R.id.logo);
    mDBHelper =new DBHelper(this);
     mError=(TextView)findViewById(R.id.mError);
    //Define the variables
    userNameEdit=(EditText)findViewById(R.id.userName);
    passwordEdit=(EditText)findViewById(R.id.password);
    _signUp=(TextView)findViewById(R.id.sginUp);
    //End



    _signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

           intent=new Intent(MainActivity.this,Signup.class);
           startActivity(intent);

        }
    });
   }

1 个答案:

答案 0 :(得分:1)

我通过用LinearLayout包围按钮来解决

<LinearLayout
    android:id="@+id/buttonContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/password"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
   >

    <Button
        android:id="@+id/logIn"
        android:layout_width="186dp"
        android:layout_height="wrap_content"
        android:layout_marginVertical="12dp"
        android:background="@drawable/btn_rounded"
        android:minWidth="200dp"
        android:onClick="logIN"
        android:text="@string/title_login"
        android:textAllCaps="false"
        android:textColor="@color/ms_white"
        android:textSize="18sp" />

</LinearLayout>