TabLayout下Fragment底部跑马灯TextView不显示问题排查
问题分析与解决方案:TabLayout Fragment中跑马灯TextView运行不显示
我来帮你排查这个问题,结合你提供的代码,大概率是布局依赖错误或者视图层级/焦点问题导致的,以下是具体分析和解决办法:
问题描述
在使用TabLayout时,第二个Fragment中添加的带跑马灯效果的TextView(id:scrollingtext)在布局编辑器中可见,但运行后消失,其他界面元素显示正常。
你的布局代码
Fragment XML代码
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".BlankFragment2"> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ScrollView01" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/arkaplan" android:fillViewport="true" android:scrollbars="none" tools:layout_editor_absoluteX="0dp" tools:layout_editor_absoluteY="0dp"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fbutton="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <RelativeLayout android:id="@+id/ustaraf" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:background="@android:color/transparent"> <LinearLayout android:id="@+id/ustbutonliner" android:layout_width="280dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="15dp" android:orientation="horizontal"> <Button android:id="@+id/kredial" android:layout_width="120dp" android:layout_height="wrap_content" android:theme="@style/AppTheme" android:text="@string/buycredit" android:textColor="@android:color/white" /> <Button android:id="@+id/kazanButton" android:layout_width="150dp" android:layout_height="wrap_content" android:textAllCaps="false" android:theme="@style/AppTheme" android:text="@string/wincredis" android:textColor="@android:color/white" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/reklamlayout2" android:layout_width="match_parent" android:layout_height="270dp" android:layout_below="@+id/ustaraf" android:layout_centerHorizontal="true" android:layout_marginLeft="7dp" android:layout_marginTop="15dp" android:background="@android:color/transparent"> <WebView android:id="@+id/reklamsa2" android:layout_width="match_parent" android:layout_height="270dp" android:layout_centerInParent="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/boslukmacs" android:layout_width="match_parent" android:layout_height="80dp" android:layout_below="@+id/maç15" android:layout_centerHorizontal="true" android:layout_marginLeft="7dp" android:layout_marginTop="15dp" android:background="@android:color/transparent"> </RelativeLayout> </RelativeLayout> </ScrollView> <RelativeLayout android:id="@+id/Kredits" android:layout_width="70dp" android:layout_height="60dp" android:layout_marginTop="15dp" android:layout_alignParentLeft="true" android:background="#FFDE00"> <LinearLayout android:id="@+id/soldaya" android:layout_width="8dp" android:layout_height="60dp" android:layout_alignParentLeft="true" android:background="@android:color/holo_orange_dark"> </LinearLayout> <LinearLayout android:id="@+id/vipsim" android:layout_width="70dp" android:layout_height="60dp" android:layout_alignParentLeft="true" android:layout_marginLeft="7dp" android:visibility="invisible" android:background="@drawable/ok"> </LinearLayout> <TextView android:id="@+id/kredimiz" android:layout_width="29dp" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:text="50" android:textAlignment="center" android:textSize="25dp" android:textStyle="bold" /> <TextView android:id="@+id/krediyazi" android:layout_width="60dp" android:layout_height="wrap_content" android:text="@string/credit" android:textAlignment="center" android:layout_marginTop="30dp" android:textSize="15dp" android:textStyle="bold" android:layout_marginLeft="13dp"/> </RelativeLayout> <TextView android:id="@+id/scrollingtext" android:layout_width="match_parent" android:layout_height="42dp" android:layout_gravity="bottom|center_horizontal" android:background="@color/colorPrimary" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:singleLine="true" android:textColor="#FFFFFF" android:textSize="25dp" android:text="TEST TEST"/> </FrameLayout>
TabLayout XML代码
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".tabdene"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="@dimen/appbar_padding_top" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_weight="1" android:background="?attr/colorPrimary" app:layout_scrollFlags="scroll|enterAlways" app:popupTheme="@style/AppTheme.PopupOverlay" app:title="OPEN SIGNALS"> </android.support.v7.widget.Toolbar> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TabItem android:id="@+id/tabItem" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tab_text_1" /> <android.support.design.widget.TabItem android:id="@+id/tabItem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tab_text_2" /> </android.support.design.widget.TabLayout> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout>
问题排查与解决方案
1. 最可能的原因:无效的RelativeLayout布局依赖
在你的Fragment布局中,ScrollView内部的boslukmacs布局设置了android:layout_below="@+id/maç15",但整个布局中不存在id为maç15的视图。这种无效的布局依赖会导致RelativeLayout的布局计算异常,可能使得ScrollView的内容高度超出预期,或者布局层级混乱,从而覆盖了底部的scrollingtext。
解决办法:
- 移除
android:layout_below="@+id/maç15"属性,或者添加对应的maç15视图,确保RelativeLayout的布局依赖有效。
2. 跑马灯TextView需要焦点才能正常显示
跑马灯效果的TextView默认需要获取焦点才能激活,即使文本长度足够,如果焦点被其他视图抢占,也可能导致文本不显示(尤其是在Fragment初始化时)。
解决办法:
- 在
scrollingtext的XML属性中添加以下两行:android:focusable="true" android:focusableInTouchMode="true" - 或者在Fragment的
onViewCreated方法中主动请求焦点:@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); TextView scrollingText = view.findViewById(R.id.scrollingtext); scrollingText.requestFocus(); }
3. 视图层级覆盖问题
虽然FrameLayout的子视图按添加顺序叠放(scrollingtext在最后应该处于最上层),但ScrollView的fillViewport="true"可能导致其内容填充整个视图区域,视觉上覆盖了底部的TextView。
解决办法:
- 给
scrollingtext添加android:elevation="4dp"属性,提升其层级优先级,确保它显示在其他视图之上:android:elevation="4dp"
4. 临时测试:排查文本显示问题
如果以上方法都无效,可以暂时移除跑马灯相关属性,看文本是否能正常显示,以此缩小排查范围:
<!-- 临时修改,测试文本是否显示 --> <TextView android:id="@+id/scrollingtext" android:layout_width="match_parent" android:layout_height="42dp" android:layout_gravity="bottom|center_horizontal" android:background="@color/colorPrimary" android:textColor="#FFFFFF" android:textSize="25dp" android:text="TEST TEST"/>
如果修改后文本显示,说明问题出在跑马灯属性的配置上,再结合焦点设置调整即可。
内容的提问来源于stack exchange,提问作者Dorbagna
相关产品推荐
相关产品推荐

