gpt4 book ai didi

Android fragment 重叠

转载 作者:行者123 更新时间:2023-11-29 16:00:17 39 4
gpt4 key购买 nike

在我的 Android 应用程序中,我有一个编辑文本和一个按钮,单击该按钮会向我的主要 Activity 添加一个 fragment ,其中包含在我的编辑文本中写入的消息。问题是,当我更改消息并单击按钮时, fragment 会重叠。我已经尝试为我的 fragment 布局添加背景,但是当我这样做时,背景出现在所有消息的顶部( TextView 甚至不可见,只能选择用于复制/粘贴)。

代码:

添加 fragment 的主 Activity 中的方法:

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();
}

fragment :

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);
}
}

fragment 的布局:

<?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 添加到下一行的 fragment 容器(FrameLayout)?

container.addView(textView);

如果我是,这会导致问题吗?

最佳答案

已解决:

问题是我将 TextView 添加到 fragment 的容器(一个 FrameLayout),我通过将 TextView 添加到 Fragmet 的布局并动态更改其文本解决了这个问题。

代码:

fragment :

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;
}

fragment 的布局:

<?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>

关于Android fragment 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25110067/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com