在我的Android应用程序中,我有一个编辑文本和一个按钮,单击该按钮会在我的主活动中添加一个片段,其中包含在我的编辑文本中写入的消息。问题是,当我更改消息并单击按钮时,片段重叠。 我已经尝试为我的片段布局添加背景但是当我这样做时,背景出现在所有消息的顶部(文本视图甚至不可见,只能选择复制/粘贴)。
守则:
主要活动中添加片段的方法:
public void viewMessage(View view)
{
// Create a new Fragment to be placed in the activity layout
ViewMessageFragment fragment = new ViewMessageFragment();
//Add the message to the fragment
//Get the message
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
//Add it to the fragment
Bundle bundle = new Bundle();
bundle.putString(EXTRA_MESSAGE, message);
fragment.setArguments(bundle);
// Add the fragment to the 'view_message' FrameLayout
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.view_message, fragment);
transaction.commit();
}
片段:
public class ViewMessageFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Get the message
String message = getArguments().getString(MainActivity.EXTRA_MESSAGE);
//Create the TextView
TextView textView = new TextView(container.getContext());
textView.setTextSize(20);
textView.setGravity(Gravity.CENTER);
textView.setWidth(-1);//This line is the same than android:layout_width="match_parent"
textView.setHeight(-1);//This line is the same than android:layout_height="match_parent"
textView.setTextIsSelectable(true);
textView.setText(message);
//Add the TextView
container.addView(textView);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_view_message, container, false);
}
}
片段的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
如果有人能帮助我,我真的很感激!谢谢!
编辑:我是否在下一行将textView添加到片段容器(FrameLayout)?
container.addView(textView);
如果我,那会导致问题吗?
答案 0 :(得分:3)
<强>解决:强>
问题在于我将TextView添加到片段的容器(FrameLayout)中,我解决了它将TextView添加到Fragmet的布局并动态更改其文本。
守则:
片段:
public class ViewMessageFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
//Get the message
String message = getArguments().getString(MainActivity.EXTRA_MESSAGE);
//Inflate the layout in a View. This way you get the fragments layout
View mContainer = inflater.inflate(R.layout.fragment_view_message, container, false);
//Find the TextView contained in the fragments Layout and change its message
TextView textView = (TextView) mContainer.findViewById(R.id.show_message);
textView.setText(message);
//Return the layout
return mContainer;
}
片段的布局:
<?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" >
<TextView android:id="@+id/show_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="true"/>
</LinearLayout>
答案 1 :(得分:1)
在这里我找到了更好的解决方案: 好吧设置片段背景颜色不是解决方案,因为片段仍然在活动堆栈中,可能会消耗内存。
解决方案是在添加任何新片段之前从framelayout中删除所有视图。
private void changeFragment(Fragment fr){
FrameLayout fl = (FrameLayout) findViewById(R.id.mainframe);
fl.removeAllViews();
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
transaction1.add(R.id.mainframe, fr);
transaction1.commit();}