为什么不能显示片段布局? [英] Why fragment layout cannot be displayed?
本文介绍了为什么不能显示片段布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有什么遗漏了吗?我想开 edit_information
片段从修改
活动。它可以显示在吐司
,但不显示 edit_information
布局。
修改
listViewEdit.setOnItemClickListener(新AdapterView.OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<>的ListView,观景,
INT位置,长的id){
//获取光标,定位到相应listview_item_row结果集中
光标光标=(光标)listView.getItemAtPosition(位置); //从这个listview_item_row数据库获取国家的首都。
字符串ID =
cursor.getString(cursor.getColumnIndexOrThrow(_ ID));
EditInformation fragment2 =新EditInformation();
束束=新包();
bundle.putString(ID,ID);
fragment2.setArguments(包);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1,fragment2);
fragmentTransaction.addToBackStack(NULL);
fragmentTransaction.commit(); }
});
EditInformation
公共类EditInformation扩展片段{ 字符串ID; 公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
查看edit_info = inflater.inflate(R.layout.edit_information,集装箱,FALSE);
捆绑包= this.getArguments();
如果(getArguments()!= NULL)
{
ID = bundle.getString(ID);
} Toast.makeText(getActivity(),Edit_Information,Toast.LENGTH_LONG).show();
返回edit_info;
}
}
修改
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直的android:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID /片段1> < RelativeLayout的
机器人:layout_width =209dp
机器人:layout_height =WRAP_CONTENT> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@彩色/番茄
机器人:paddingLeft =10SP
机器人:TEXTSIZE =20SP
机器人:paddingTop =10SP
机器人:提示=龙preSS删除/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字颜色=@彩色/番茄
机器人:paddingTop =40SP
机器人:TEXTSIZE =20SP
机器人:paddingLeft =10SP
机器人:提示=提示编辑/> < / RelativeLayout的> < ListView控件
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:ID =@ + ID / listViewEdit
机器人:paddingTop =10SP/>
< / LinearLayout中>
edit_information
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:填充=10SP
机器人:方向=垂直
机器人:背景=@色/米色> < RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT> <的TextView
机器人:ID =@ + ID /名称
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ round_corner_up
机器人:填充=10dp
机器人:文字=姓名
机器人:文字颜色=@彩色/番茄
机器人:TEXTSIZE =20dp/> <的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /名称
机器人:layout_marginTop = - 15dp
机器人:背景=@绘制/ round_corner_up> <微调
机器人:layout_width =339dp
机器人:layout_height =52dp
机器人:ID =@ + ID / SpinnerName/> < / LinearLayout中>
< / RelativeLayout的> < RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =10dp> <的TextView
机器人:ID =@ + ID /天气
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@绘制/ round_corner_up
机器人:填充=10dp
机器人:文字=天气
机器人:文字颜色=@彩色/番茄
机器人:TEXTSIZE =20dp/> <的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /天气
机器人:layout_marginTop = - 15dp
机器人:背景=@绘制/ round_corner_up>
<微调
机器人:layout_width =342dp
机器人:layout_height =52dp
机器人:ID =@ + ID / SpinnerWeather> < /微调> < / LinearLayout中>
< / RelativeLayout的> < RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =10dp> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /日
机器人:背景=@绘制/ round_corner_up
机器人:填充=10dp
机器人:文字=日期
机器人:文字颜色=@彩色/番茄
机器人:TEXTSIZE =20dp/> <的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /日
机器人:layout_marginTop = - 15dp
机器人:背景=@绘制/ round_corner_up> <的EditText
机器人:layout_width =311dp
机器人:layout_height =52dp
机器人:layout_margin =10SP
机器人:背景=@绘制/ round_corner_square
机器人:ID =@ + ID /日期
机器人:paddingLeft =20dp/> < / LinearLayout中>
< / RelativeLayout的> < RelativeLayout的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =10dp> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID /状态
机器人:背景=@绘制/ round_corner_up
机器人:填充=10dp
机器人:文字=状态
机器人:文字颜色=@彩色/番茄
机器人:TEXTSIZE =20dp/> <的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID /状态
机器人:layout_marginTop = - 15dp
机器人:背景=@绘制/ round_corner_up>
<微调
机器人:layout_width =339dp
机器人:layout_height =52dp
机器人:ID =@ + ID / SpinnerStatus> < /微调>
< / LinearLayout中> < / RelativeLayout的>
< AbsoluteLayout 机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:paddingTop =20SP
机器人:方向=横向>
<按钮
机器人:layout_width =187dp
机器人:layout_weight =0.83
机器人:文字=添加工作组
机器人:TEXTSIZE =20SP
机器人:drawableRight =@的mipmap / add_workforce
机器人:layout_height =75dp
机器人:ID =@ + ID / AddForce
机器人:layout_x =12dp
机器人:layout_y =11DP/> <按钮
机器人:layout_width =123dp
机器人:layout_weight =0.83
机器人:文字=下一步
机器人:TEXTSIZE =20SP
机器人:drawableRight =@的mipmap /下一个
机器人:layout_height =71dp
机器人:ID =@ + ID /下一页
机器人:layout_x =211dp
机器人:layout_y =14dp/>
< / AbsoluteLayout>
< / LinearLayout中>
解决方案
尝试使用的FrameLayout
作为片段,而不是一个容器的LinearLayout
。我相信,因为的LinearLayout
是越来越创造出来的画面的片段。其中,为的FrameLayout
将重叠其它视图的片段。
What have I missed out here? I wanted to open edit_information
fragment from edit
activity. It can display the Toast
but not displaying the edit_information
layout.
Edit
listViewEdit.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding listview_item_row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this listview_item_row in the database.
String ID =
cursor.getString(cursor.getColumnIndexOrThrow("_id"));
EditInformation fragment2 = new EditInformation();
Bundle bundle = new Bundle();
bundle.putString("ID", ID);
fragment2.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
EditInformation
public class EditInformation extends Fragment {
String ID;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View edit_info = inflater.inflate(R.layout.edit_information, container, false);
Bundle bundle = this.getArguments();
if(getArguments()!=null)
{
ID=bundle.getString("ID");
}
Toast.makeText(getActivity(),"Edit_Information",Toast.LENGTH_LONG).show();
return edit_info;
}
}
edit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1">
<RelativeLayout
android:layout_width="209dp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tomato"
android:paddingLeft="10sp"
android:textSize="20sp"
android:paddingTop="10sp"
android:hint="Long Press to delete"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/tomato"
android:paddingTop="40sp"
android:textSize="20sp"
android:paddingLeft="10sp"
android:hint="Hint to edit"/>
</RelativeLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listViewEdit"
android:paddingTop="10sp"/>
</LinearLayout>
edit_information
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10sp"
android:orientation="vertical"
android:background="@color/beige">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Name"
android:textColor="@color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Name"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="@+id/SpinnerName" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="@+id/Weather"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Weather"
android:textColor="@color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Weather"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_width="342dp"
android:layout_height="52dp"
android:id="@+id/SpinnerWeather">
</Spinner>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Date"
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Date"
android:textColor="@color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Date"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<EditText
android:layout_width="311dp"
android:layout_height="52dp"
android:layout_margin="10sp"
android:background="@drawable/round_corner_square"
android:id="@+id/date"
android:paddingLeft="20dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Status"
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Status"
android:textColor="@color/tomato"
android:textSize="20dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Status"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_width="339dp"
android:layout_height="52dp"
android:id="@+id/SpinnerStatus">
</Spinner>
</LinearLayout>
</RelativeLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:orientation="horizontal">
<Button
android:layout_width="187dp"
android:layout_weight="0.83"
android:text="Add Work Force"
android:textSize="20sp"
android:drawableRight="@mipmap/add_workforce"
android:layout_height="75dp"
android:id="@+id/AddForce"
android:layout_x="12dp"
android:layout_y="11dp" />
<Button
android:layout_width="123dp"
android:layout_weight="0.83"
android:text="Next"
android:textSize="20sp"
android:drawableRight="@mipmap/next"
android:layout_height="71dp"
android:id="@+id/Next"
android:layout_x="211dp"
android:layout_y="14dp" />
</AbsoluteLayout>
</LinearLayout>
解决方案
Try using FrameLayout
as a container for fragment instead of LinearLayout
. I believe because of LinearLayout
your fragment is getting created out of screen. where as FrameLayout
will overlap your fragment on other Views.
这篇关于为什么不能显示片段布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文