美好的一天
我是Android开发的新手,我正在尝试在主xml文件中为片段添加2个容器,每个用户都会使用横向屏幕的一半,但我尝试的所有内容都不起作用,请帮忙。
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.newProject.MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="221dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/fragments_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
谢谢你!
答案 0 :(得分:4)
我希望这对你有用
对于横向模式
<?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:weightSum="2"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/fragments_container1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
答案 1 :(得分:1)
在xml中添加
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:orientation="vertical"
android:paddingRight="20dp"
android:paddingTop="20dp"
android:weightSum="2"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/frame1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
<FrameLayout
android:id="@+id/frame2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
活动类中的:
public void loadFragments(){
Fragment1 fragment1 = new Fragment1();
Fragment2 fragment2 = new Fragment2();
loadFragment(this,fragment1);
loadFragmentTwo(this,fragment2);
}
public static void loadFragment(AppCompatActivity activity, Fragment newFragment) {
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame1, newFragment, getTagForFragment(newFragment)).commitAllowingStateLoss();
}
public static void loadFragmentTwo(AppCompatActivity activity, Fragment newFragment) {
activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame2, newFragment, getTagForFragment(newFragment)).commitAllowingStateLoss();
}
public static String getTagForFragment(Fragment fragment) {
Log.d("fragment name", "fragment name " + fragment.getClass().getSimpleName());
return fragment.getClass().getSimpleName();
}
答案 2 :(得分:1)
试试这个xml代码:
<android.support.constraint.ConstraintLayout
android:id="@+id/main_content"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<FrameLayout
android:id="@+id/frameLayout2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frameLayout2"/>
</android.support.constraint.ConstraintLayout>