我试图在宽度小于500px的屏幕上隐藏两个TextView。
我尝试了以下(这不是我的完整代码,但必不可少):
public class HeaderFooterFragment extends Fragment {
private TextView lable;
private TextView app;
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
// some code...
super.onActivityCreated(savedInstanceState);
}
public void setLableInvisible()
{
lable.setVisibility(View.INVISIBLE);
app.setVisibility(View.INVISIBLE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
}
我的XML是:
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/toolbar_height"
android:id="@+id/lable"
android:layout_weight="0.3"
android:textAlignment="gravity"
android:gravity="left"
android:text="@string/app_name"
android:textSize="@dimen/site_fontsize"
android:paddingTop="@dimen/site_paddingtop"
android:paddingLeft="@dimen/site_paddingleft"
android:textColor="@color/site_color"
android:scaleType="fitCenter"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="@dimen/toolbar_height"
android:layout_weight="0.05"
android:id="@+id/app"
android:textAlignment="gravity"
android:gravity="left"
android:text="@string/app_name_extra"
android:textSize="@dimen/apptitlex_fontsize"
android:paddingTop="@dimen/apptitlex_paddingtop"
android:paddingLeft="@dimen/apptitlex_paddingleft"
android:textColor="@color/colorAccent"
android:textAllCaps="true"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:visibility="visible"
/>
当我在宽度为480px的智能手机上启动应用程序时,我得到一个空对象引用。如果我在更大的屏幕上启动应用程序,应用程序不会崩溃。
提前致谢。
答案 0 :(得分:1)
我不确定在OnActivityCreated中是否已创建视图,请尝试在OnCreateView中初始化textView
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
答案 1 :(得分:0)
try this and delete onactivitycreated method
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
答案 2 :(得分:0)
声明 TextView onCreateView
而不是onActivityCreated
部分
View RootView; //Declare Global
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
RootView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) RootView.findViewById(R.id.lable);
app = (TextView) RootView.findViewById(R.id.app);
// Add Your staff here
return RootView;
}
答案 3 :(得分:0)
而不是这个
setLableInvisible();
使用以下代码隐藏textview
app.setVisibility(View.INVISIBLE)
同样将其用于第二次textview。 感谢