我正在尝试在活动上方添加片段。我在主容器上添加了片段,但不是ovrlapping Activity屏幕,而是片段位于mainActivity的布局下面。我知道这是一个非常愚蠢的问题,但无法找到解决方案。下面我发布我的代码。需要你的建议。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
EditText email, password;
TextView login,signup;
public FragmentManager fragmentManager;
public FragmentTransaction fragmentTransaction;
Fragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
email = (EditText) findViewById(R.id.emailVal);
password = (EditText) findViewById(R.id.passwordVal);
login = (TextView) findViewById(R.id.login);
signup = (TextView) findViewById(R.id.signUP);
fragment = new SignupFragment();
signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.maine, fragment);
fragmentTransaction.addToBackStack("backfrag");
fragmentTransaction.commit();
}
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/maine"
tools:context="com.hubbellindia.lightdemo1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:text="LOGIN" />
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint=" Enter Email"
android:id="@+id/emailVal"
android:background="@drawable/edit"
android:layout_marginTop="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint=" Enter Password"
android:id="@+id/passwordVal"
android:background="@drawable/edit"
android:layout_marginTop="40dp"
/>
<TextView
android:layout_width="160dp"
android:layout_height="40dp"
android:text="LOGIN"
android:gravity="center"
android:background="#ee4356"
android:id="@+id/login"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"/>
<TextView
android:layout_width="160dp"
android:layout_height="40dp"
android:text="Sign up"
android:gravity="center"
android:id="@+id/signUP"
android:layout_marginLeft="80dp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>
</LinearLayout>
SignUpFragment.java:
public class SignupFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_signup, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
}
fragment_signup.xml:
<FrameLayout 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"
tools:context="com.hubbellindia.lightdemo1.SignupFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hubbellindia.lightdemo1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000000"
android:layout_gravity="center_horizontal"
android:text="SIGNUP" />
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint=" Enter Email"
android:id="@+id/emailValNew"
android:background="@drawable/edit"
android:layout_marginTop="40dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint=" Enter Password"
android:id="@+id/passwordValNew"
android:background="@drawable/edit"
android:layout_marginTop="40dp"
/>
<TextView
android:layout_width="160dp"
android:layout_height="40dp"
android:text="SIGNUP"
android:gravity="center"
android:background="#ee4356"
android:id="@+id/signup"
android:textColor="#ffffff"
android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"/>
</LinearLayout>
</FrameLayout>
答案 0 :(得分:0)
尝试将activity
布局替换为此
<FrameLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hubbellindia.lightdemo1.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="LOGIN"
android:textColor="#000000"
android:textSize="25sp" />
<EditText
android:id="@+id/emailVal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:background="@drawable/edit"
android:hint=" Enter Email"
/>
<EditText
android:id="@+id/passwordVal"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="40dp"
android:background="@drawable/edit"
android:hint=" Enter Password"
/>
<TextView
android:id="@+id/login"
android:layout_width="160dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dp"
android:background="#ee4356"
android:gravity="center"
android:text="LOGIN"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/signUP"
android:layout_width="160dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="80dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Sign up"
android:textColor="#000000"
android:textStyle="bold" />
</LinearLayout>
<FrameLayout
android:id="@+id/maine"
android:layout_width="match_parent"
android:layout_height="match_parent" />
将您的片段替换为FramLayout
- maine
以避免重叠。
答案 1 :(得分:0)
try it
**1)activity_main.xml`enter code here`**
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.admin.androidmodule.MainActivity">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="@+id/button"
android:layout_marginTop="@dimen/activity_horizontal_margin"
android:elevation="0dp" />
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</FrameLayout>
</RelativeLayout>
**2)MainActivity**
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DemoFragment demoFragment = new DemoFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container,demoFragment, "demoFragment").commit();
}
});
}
}