Android 一个活动中的三个片段
Android新手,我正在尝试制作一个显示姓名列表的应用程序,然后在右边显示一个主题标题,然后是显示该主题标题详细信息的第三个片段。我想把这三个片段都放在我的主要活动上。所以我希望它看起来像这样: 到目前为止,这是主要的xml:Android 一个活动中的三个片段,android,android-fragments,fragment,Android,Android Fragments,Fragment,Android新手,我正在尝试制作一个显示姓名列表的应用程序,然后在右边显示一个主题标题,然后是显示该主题标题详细信息的第三个片段。我想把这三个片段都放在我的主要活动上。所以我希望它看起来像这样: 到目前为止,这是主要的xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" a
<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:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/companyListFrag_LL"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class= "name.financialnews.CompaniesListFragment"
android:id="@+id/list_companies"
android:layout_width="10dp"
android:layout_weight="20"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/list_headings"
android:layout_width="20dp"
android:layout_weight="80"
android:layout_marginLeft="20dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" />
</LinearLayout>
<FrameLayout
android:id="@+id/detail_fragment"
android:layout_width="0dp"
android:layout_below="@id/list_headings"
android:layout_weight="1"
android:layout_height="wrap_content" />
</RelativeLayout>
将根元素从RelativeLayout更改为LinearLayout,并将其方向更改为垂直。我还要在内线布局上加一个权重,这样根元素的两个直接子元素的权重相等。你给公司列表和标题的空间也很小(20dp不是很大)
当我将您的代码粘贴到Android Studio时,会出现一些警告,说明您的布局存在一些问题,这些问题本可以为您指明正确的方向。总的来说,我建议从简单开始,一次添加一个片段。如果在查看布局时遇到问题,可以在开发人员选项中打开“显示布局边界”设置。将根元素从RelativeLayout更改为LinearLayout,并将其方向更改为垂直。我还要在内线布局上加一个权重,这样根元素的两个直接子元素的权重相等。你给公司列表和标题的空间也很小(20dp不是很大)
当我将您的代码粘贴到Android Studio时,会出现一些警告,说明您的布局存在一些问题,这些问题本可以为您指明正确的方向。总的来说,我建议从简单开始,一次添加一个片段。如果您在查看布局时遇到问题,可以在开发人员选项中打开“显示布局边界”设置。谢谢您的回复。我改变了你的要求,但它仍然没有显示底部的第三个片段。其他一切都很好。我无法帮助您处理该片段,因为它不在布局xml中。我假设您正在用java代码向框架布局中添加一个细节片段。我会将另一个片段添加到主帖子中。正如我之前所说,细节片段是如何插入主布局的?很抱歉,我添加了主活动的java代码谢谢您的回复。我改变了你的要求,但它仍然没有显示底部的第三个片段。其他一切都很好。我无法帮助您处理该片段,因为它不在布局xml中。我假设您正在用java代码向框架布局中添加一个细节片段。我将向主Post中添加另一个片段正如我之前所说,细节片段是如何插入主布局的?很抱歉,我为主活动添加了java代码
<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"
android:orientation="vertical"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_marginTop="30dp"
android:id="@+id/textHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
/>
<TextView
android:id="@+id/textDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</FrameLayout>
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Fragment;
public class MainActivity extends AppCompatActivity
implements CompaniesListFragment.OnCompaniesSelectedListener,
HeadingsListFragment.OnHeadingsSelectedListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onCompanySelected(long id){
HeadingsListFragment headingsList = new HeadingsListFragment();
FragmentTransaction transaction=
getFragmentManager().beginTransaction();
headingsList.updateCompanyId(id);
transaction.replace(R.id.list_headings,headingsList);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
@Override
public void onHeadingSelected(long id) {
Detail_Fragment items = new Detail_Fragment();
FragmentTransaction transaction=
getFragmentManager().beginTransaction();
items.updateHeadingId(id);
transaction.replace(R.id.detail_fragment, items);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
}