當我在帶有 ScrollView 的 Fragment 內使用 Fragment 時它不起作用。在我的情況下FirstFragment很好。里面的片段沒有全屏顯示,里面的很多內容BooksFragment都消失了
這是 MyFirstFragment的 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DADADA"
tools:context=".FirstFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:id="@ id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
app:tabGravity="fill"
app:tabMode="fixed">
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@ id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tabLayout"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
這是 MyFirstFragment的 JAVA :
public class FirstFragment extends Fragment {
public FirstFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
static class ViewPagerAdapter extends FragmentStateAdapter {
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
super(fragmentManager, lifecycle);
}
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
return fragmentList.size();
}
}
ViewPagerAdapter adapter;
ViewPager2 viewPager;
TabLayout tabLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_first, container, false);
viewPager = view.findViewById(R.id.viewPager);
tabLayout = view.findViewById(R.id.tabLayout);
initView();
return view;
}
private void initView() {
adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(), getActivity().getLifecycle());
adapter.addFragment(new BooksFragment(), "Books");
adapter.addFragment(new ComputerFragment(), "Computers");
viewPager.setAdapter(adapter);
new TabLayoutMediator(tabLayout, viewPager, ((tab, position) -> {
tab.setText(adapter.fragmentTitleList.get(position));
})).attach();
}
}
這是 MyBooksFragment的 Xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BooksFragment">
<TextView
android:id="@ id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akdlorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd lorem ipsum lorem ipsum how sldj as kye akd"
android:lineSpacingExtra="20dp"
android:textSize="20sp"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
如果我用簡單的話來說這個問題,它會是這樣的:
我的第一個 Fragment 很好,但內部 Fragment 的內容沒有顯示,所以我該如何解決?任何幫助將不勝感激。如果您對我的帖子有任何疑問或問題,可以告訴我。
例子 :
正如你們已經知道的那樣,我BooksFragment的 Xml 中有很長的文本:但是在此影像中,您明白了。
在此影像中,甚至沒有顯示一個文本

在此影像中顯示文本是因為它的文本長度非常小。

警報:
請不要認為灰色背景是他的(
booksFragment,ComputerFragment)。FirstFragment的Bg是灰色的!不BooksFragment和ComputerFragment
編輯:
我用不同的顏色添加這個影像,所以你得到什么是什么大小。

這里的顏色定義
- 灰色 -
MainActivity - 淺藍色 -
FirstFragment - 紅色 -
BookFragment
uj5u.com熱心網友回復:
您的代碼作業正常,但您的代碼有一些小錯誤,因此您可以遵循此代碼 -
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#BBF8FF"
tools:context=".FirstFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.tabs.TabLayout
android:id="@ id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
app:tabMode="fixed"/>
<androidx.viewpager2.widget.ViewPager2
android:id="@ id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/tabLayout"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331650.html
標籤:爪哇 安卓 xml android-fragments 滚动视图
上一篇:如何在Android應用程式中保存上次輸入的編輯文本值?
下一篇:Python從xml請求中提取
