首先贴上布局代码
<?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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_fans"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#f8f8f8">
<TextView
android:id="@+id/tv_fans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="15dp"
android:text="20位粉丝"
android:textColor="#999"
android:textSize="@dimen/text_size_13" />
</LinearLayout>
<com.tg.star.view.PullRefreshListView
android:id="@+id/lv_fans"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.tg.star.view.PullRefreshListView>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_no_fans"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/no_concern_you" />
</LinearLayout>
</LinearLayout>
结果是隐藏了上面的布局,下面的图片还是不能居中显示。
然后我们来查看fragment中的代码
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_fans, null);
initView();
initData();
return mView;
}
注意我们加粗的地方,通过查官方文档,如果传入null,在xml文件中设置的布局参数都会丢失,android会采用默认的布局参数。
所以要修改为
mView = inflater.inflate(R.layout.fragment_fans, container,false);
当attachToRoot传入false值时,ViewGroup root用来保存布局参数