我的activity_main.xml
使用LinearLayout
以3种方式垂直分割。我想用LinearLayout
填充底部ListFragment
,并用数据填充它。
目前,ListFragment
会忽略3 LinearLayout
并将其置于最重要的位置。
LstFragment.java
public class LstFragment extends ListFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragmentlayout, container, false);
// Create an array of string to be data source of the ListFragment
String[] datasource={"English","French","Khmer","Japanese","Russian","Chinese"};
// Create ArrayAdapter object to wrap the data source
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.rowlayout,R.id.txtitem,datasource);
// Bind adapter to the ListFragment
setListAdapter(adapter);
// Retain the ListFragment instance across Activity re-creation
setRetainInstance(true);
return rootView;
}
// Handle Item click event
public void onListItemClick(ListView l, View view, int position, long id){
ViewGroup viewg=(ViewGroup)view;
TextView tv=(TextView)viewg.findViewById(R.id.txtitem);
Toast.makeText(getActivity(), tv.getText().toString(),Toast.LENGTH_LONG).show();
}
}
MainActivity.java
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LstFragment lstfragment=(LstFragment)getSupportFragmentManager().findFragmentByTag("lstfragment");
if(lstfragment==null){
lstfragment=new LstFragment();
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(android.R.id.content,lstfragment,"lstfragment");
transact.commit();
}
}
}
activity_main.xml中
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/topSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Testing"
android:id="@+id/textView3" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/middleSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Yo"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:id="@+id/bottomSection">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="WhatUP"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>
还有两个名为.xml
的简单fragmentlayout.xml
布局文件,其中包含一个简单的ListView
@id/android:list
和一个rowlayout.xml
,其中只有一个TextView
} @id/textitem
。
答案 0 :(得分:1)
替换
android:layout_height="fill_parent"
这一行
android:layout_height="0dp"
在所有三个子线性布局中
答案 1 :(得分:0)
<强>原因强>
python manage.py migrate
为您提供视图的根元素,而无需知道其实际名称/类型/ ID。这就是为什么你的android.R.id.content
出现在一切的顶端。
您正在尝试将Fragment
添加到错误的容器
Fragment
<强>解决方案强>
添加此
transact.add(android.R.id.content,lstfragment,"lstfragment");
在第3 <FrameLayout
android:layout_width="fill_parent"
android:id="@+id/fragment_container"
android:layout_height="fill_parent">
</FrameLayout>
之后但在关闭根LinearLayout
之前
然后改为
LinerarLayout
此外,
所有transact.add(R.id.fragment_container,lstfragment,"lstfragment");
中的都使用此LinearLayout