Android 使碎片膨胀
这是我的活动课。我正试图将碎片膨胀到这个活动中Android 使碎片膨胀,android,android-fragments,Android,Android Fragments,这是我的活动课。我正试图将碎片膨胀到这个活动中 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fg1, new ButtonsFragment());
ft.commit();
}
活动\u main.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/a_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1"
/>
</LinearLayout>
<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.example.iiitb.patientdoctormanagementsystem.ButtonsFragment">
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textAllCaps="false"
android:text="Past Appointments"
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:textAllCaps="false"
android:text="Todays Appointments"
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:textAllCaps="false"
android:text="Upcoming Appointments"
android:id="@+id/b3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
片段按钮.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/a_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1"
/>
</LinearLayout>
<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.example.iiitb.patientdoctormanagementsystem.ButtonsFragment">
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:textAllCaps="false"
android:text="Past Appointments"
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:textAllCaps="false"
android:text="Todays Appointments"
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:textAllCaps="false"
android:text="Upcoming Appointments"
android:id="@+id/b3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
请帮忙 尝试在activity_main.xml中使用FrameLayout而不是fragment //activity_main.xml
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/a_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1"
/>
</LinearLayout>
有两种方法可以将片段添加到活动布局中:
<fragment
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1"
/>
- 在活动的布局文件中声明片段
- 以编程方式将片段添加到视图组
<fragment
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1"
/>
当系统创建此活动布局时,它实例化布局中指定的每个片段,并为每个片段调用onCreateView()方法,以检索每个片段的布局。系统直接插入片段返回的视图来代替元素。(来自)
通过采用这种方法,您不需要这些代码行(将其删除):
如果要采用第二种方法,请以编程方式将片段添加到现有的视图组中,应按如下所示更改活动布局:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1" />
并保持代码的其他部分不变。有关您可以参考android的片段的更多信息,请阅读错误,它已经告诉您问题:XML第7行中的错误。好,请接受答案以结束此问题,好吗?
<FrameLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="10dp"
android:id="@+id/fg1" />