我是Android开发的新手,我在片段方面遇到了一些麻烦。我有一个活动,活动的布局有两个FrameLayout
,一个在顶部,一个在底部,作为我想要添加的两个片段的容器。我想在顶部添加的片段是CalculatorFragment
,我想在底部添加的片段是AdvFragment
。 CalculatorFragment
使用RelativeLayout
; AdvFragment
使用FrameLayout
。
每当我在代码顶部添加CalculatorFragment
和在代码底部添加AdvFragment
时,当我运行它时,只有CalculatorFragment
显示在顶部; AdvFragment
未显示在底部。但是,如果我在我的代码中切换它们并在顶部添加AdvFragment
并在底部添加CalculatorFragment
,则它们都会在执行时显示正确定位。如果我只在底部添加AdvFragment
而不在我的代码顶部添加CalculatorFragment
,则AdvFragment
会在程序运行时显示正确的位置。因此,只要在顶部添加CalculatorFragment
,即使片段已添加到代码中,也不会在其下方显示任何片段。关于如何解决这个问题的任何想法?
以下是相关代码:
MainActivity.java(在onCreate()方法中):
//Adding calcfragment on top and advfragment on bottom
setContentView(R.layout.activity_main);
CalculatorFragment calcfragment = new CalculatorFragment();
AdvFragment advFragment = new AdvFragment();
f_manager = getSupportFragmentManager();
FragmentTransaction f_transaction = f_manager.beginTransaction();
f_transaction.add(R.id.main_frame, calcfragment);
f_transaction.add(R.id.advanced_frame, advFragment);
f_transaction.commit();
MainActivity的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: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.rm1399.myfirstapplication.MainActivity"
android:background="@color/highlighted_text_material_light"
android:clickable="true"
android:orientation="vertical"
>
<FrameLayout
android:id = "@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
>
</FrameLayout>
<FrameLayout
android:id = "@+id/advanced_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
</LinearLayout>
CalculatorFragment的xml布局:
<?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: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.rm1399.myfirstapplication.MainActivity"
android:background="@color/highlighted_text_material_light"
android:clickable="true"
android:orientation="horizontal"
>
<Button
android:layout_width="110dp"
android:layout_height="50dp"
android:id="@+id/calculate"
android:text="Calculate"
android:layout_below="@+id/button8"
android:layout_alignRight="@+id/button3"
android:layout_alignEnd="@+id/button3"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="@+id/button0"
android:layout_marginTop="0dp"
android:layout_toLeftOf="@+id/button1"
android:layout_below="@+id/result"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button1"
android:layout_alignTop="@+id/button0"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button2"
android:layout_below="@+id/button0"
android:layout_toLeftOf="@+id/button3"
android:layout_toStartOf="@+id/button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button3"
android:layout_alignTop="@+id/button2"
android:layout_alignLeft="@+id/button1"
android:layout_alignStart="@+id/button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button4"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
android:id="@+id/button5"
android:layout_below="@+id/button3"
android:layout_alignRight="@+id/calculate"
android:layout_alignEnd="@+id/calculate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:id="@+id/button6"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="@+id/button7"
android:layout_alignTop="@+id/button6"
android:layout_alignLeft="@+id/button5"
android:layout_alignStart="@+id/button5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="@+id/button8"
android:layout_below="@+id/button6"
android:layout_alignLeft="@+id/button6"
android:layout_alignStart="@+id/button6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="@+id/button9"
android:layout_below="@+id/button7"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/plus"
android:layout_above="@+id/button5"
android:layout_alignLeft="@+id/multiply"
android:layout_alignStart="@+id/multiply"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/minus"
android:layout_alignTop="@id/plus"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/multiply"
android:layout_toEndOf="@+id/multiply"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/multiply"
android:layout_below="@+id/plus"
android:layout_toRightOf="@+id/imageView"
android:layout_toEndOf="@+id/imageView"
android:layout_marginLeft="36dp"
android:layout_marginStart="36dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/divide"
android:layout_below="@+id/minus"
android:layout_toRightOf="@+id/multiply"
android:layout_toEndOf="@+id/multiply"/>
<TextView
android:layout_width="700dp"
android:layout_height="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/result"
android:hint = "Result"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:background="#000000"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:paddingTop="10dp"
android:paddingLeft="10dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/button1"
android:layout_alignEnd="@+id/button1"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:layout_below="@+id/divide"
android:layout_toLeftOf= "@+id/divide"
android:id="@+id/openparentheses"
android:text = "("
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:layout_toRightOf="@id/openparentheses"
android:layout_below="@id/divide"
android:text=")"
android:id="@+id/closeparentheses"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="80dp"
android:layout_height="50dp"
android:text="Clear"
android:id="@+id/clear"
android:layout_alignTop="@+id/button9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Advanced"
android:id="@+id/openadv"
android:layout_alignBottom="@+id/calculate"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<FrameLayout
android:id = "@+id/advanced_frame"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</RelativeLayout>
AdvFragment的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.example.rm1399.myfirstapplication.AdvFragment"
android:background="#FFFFFF">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="415dp"
android:textSize="100px"
android:text="Advanced Fragment"
android:backgroundTint="#FFFFFF"
android:autoText="false"/>
</FrameLayout>
在CalculatorFragment.java和AdvFragment.java中,我使用inflater.inflate()返回适当的布局。
Upon execution, I am seeing this
正如您所看到的,即使我在CalculatorFragment下面的代码中添加了AdvFragment,它也不会在执行时出现。再说一遍,任何想法?
答案 0 :(得分:0)
在mainactivity的xml中,在linearlayout中写下以下行
android:weightSum="3"
然后检查它是否有帮助。