为啥无法显示片段布局?
Posted
技术标签:
【中文标题】为啥无法显示片段布局?【英文标题】:Why fragment layout cannot be displayed?为什么无法显示片段布局? 【发布时间】:2016-03-08 22:15:10 【问题描述】:我在这里错过了什么?我想从edit
activity打开edit_information
fragment。它可以显示Toast
,但不显示edit_information
布局。
编辑
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();
);
编辑信息
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;
编辑
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_
android:layout_
android:id="@+id/fragment1">
<RelativeLayout
android:layout_
android:layout_>
<TextView
android:layout_
android:layout_
android:textColor="@color/tomato"
android:paddingLeft="10sp"
android:textSize="20sp"
android:paddingTop="10sp"
android:hint="Long Press to delete"/>
<TextView
android:layout_
android:layout_
android:textColor="@color/tomato"
android:paddingTop="40sp"
android:textSize="20sp"
android:paddingLeft="10sp"
android:hint="Hint to edit"/>
</RelativeLayout>
<ListView
android:layout_
android:layout_
android:id="@+id/listViewEdit"
android:paddingTop="10sp"/>
</LinearLayout>
编辑信息
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:padding="10sp"
android:orientation="vertical"
android:background="@color/beige">
<RelativeLayout
android:layout_
android:layout_>
<TextView
android:id="@+id/Name"
android:layout_
android:layout_
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Name"
android:textColor="@color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_
android:layout_
android:layout_below="@+id/Name"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_
android:layout_
android:id="@+id/SpinnerName" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:paddingTop="10dp">
<TextView
android:id="@+id/Weather"
android:layout_
android:layout_
android:background="@drawable/round_corner_up"
android:padding="10dp"
android:text="Weather"
android:textColor="@color/tomato"
android:textSize="20dp" />
<LinearLayout
android:layout_
android:layout_
android:layout_below="@+id/Weather"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_
android:layout_
android:id="@+id/SpinnerWeather">
</Spinner>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:paddingTop="10dp">
<TextView
android:layout_
android:layout_
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_
android:layout_
android:layout_below="@+id/Date"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<EditText
android:layout_
android:layout_
android:layout_margin="10sp"
android:background="@drawable/round_corner_square"
android:id="@+id/date"
android:paddingLeft="20dp" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_
android:layout_
android:paddingTop="10dp">
<TextView
android:layout_
android:layout_
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_
android:layout_
android:layout_below="@+id/Status"
android:layout_marginTop="-15dp"
android:background="@drawable/round_corner_up">
<Spinner
android:layout_
android:layout_
android:id="@+id/SpinnerStatus">
</Spinner>
</LinearLayout>
</RelativeLayout>
<AbsoluteLayout
android:layout_
android:layout_
android:paddingTop="20sp"
android:orientation="horizontal">
<Button
android:layout_
android:layout_weight="0.83"
android:text="Add Work Force"
android:textSize="20sp"
android:drawableRight="@mipmap/add_workforce"
android:layout_
android:id="@+id/AddForce"
android:layout_x="12dp"
android:layout_y="11dp" />
<Button
android:layout_
android:layout_weight="0.83"
android:text="Next"
android:textSize="20sp"
android:drawableRight="@mipmap/next"
android:layout_
android:id="@+id/Next"
android:layout_x="211dp"
android:layout_y="14dp" />
</AbsoluteLayout>
</LinearLayout>
【问题讨论】:
你在 replace() 方法中使用的“R.id.fragment1”是什么? 尝试将您的 LinearLayout 包装在 Scrollview 中的 edit.xml 中,以查看您的片段是否显示在 ListView 下方 如何包装线性布局? 您只需要使用 Scrollview 元素作为布局的根 看到这个答案它会帮助你http://***.com/questions/10419924/android-adding-a-fragment-to-an-activity 【参考方案1】:尝试使用FrameLayout
作为片段的容器,而不是LinearLayout
。我相信由于LinearLayout
,您的片段正在屏幕外创建。其中FrameLayout
将在其他视图上重叠您的片段。
【讨论】:
使用 FragmentLayout 而不是 Linear ? 它的 FrameLayout 不是 FragmentLayout以上是关于为啥无法显示片段布局?的主要内容,如果未能解决你的问题,请参考以下文章