更改片段中 LinearLayout 的权重 - Android Studio
Change Weight of a LinearLayout in a Fragment - Android Studio
(首先我必须说我的英文很一般)。
大家好,我正在尝试将 LinearLayout
的权重从 Fragment
更改。这是我的代码,但是在单击片段时它崩溃了。这个想法是,在点击片段的那一刻,它会填满移动屏幕,其他片段也是如此。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_info1, container, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
XX.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT,0));
}
});
return v;
}
这是错误:
10-10 20:06:54.222 30286-30286/com.example.jhon.myapplication E/AndroidRuntime:
FATAL EXCEPTION: main: com.example.jhon.myapplication, PID: 30286
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1.onClick(Info1.java:79)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
在 xml 代码中,我尝试将每个片段放在线性布局中,以便能够更轻松地修改它们的权重。
<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"
android:orientation="vertical"
tools:context="com.example.jhon.myapplication.Informacion">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/LinearInfo1">
<fragment
android:id="@+id/fragment"
android:name="com.example.jhon.myapplication.Info1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LinearInfo2"
android:layout_weight="1">
<fragment
android:id="@+id/fragment2"
android:name="com.example.jhon.myapplication.Info2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info2" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
新Logcat信息:
10-10 21:06:03.232 10887-10887/com.example.jhon.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jhon.myapplication, PID: 10887
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1.onClick(Info1.java:80)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
尝试将 LinearLayout 引入内部匿名方法之外,并将相对布局参数切换为线性布局参数:
View v = inflater.inflate(R.layout.fragment_info1, container, false);
LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
XX.setLayoutParams(param);
}
});
return v;
在您的代码中试试这个。
private LinearLayout XX;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_info1, container, false);
// add here
XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
// edited here
if (XX != null) {
XX.setLayoutParams(param);
}
}
});
return v;
}
- 将 LinearLayout
设为全局变量 .
- 使 LinearLayout
最终
final LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
Find the corresponding ID of LinearLayout
(首先我必须说我的英文很一般)。
大家好,我正在尝试将 LinearLayout
的权重从 Fragment
更改。这是我的代码,但是在单击片段时它崩溃了。这个想法是,在点击片段的那一刻,它会填满移动屏幕,其他片段也是如此。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_info1, container, false);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
XX.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT,0));
}
});
return v;
}
这是错误:
10-10 20:06:54.222 30286-30286/com.example.jhon.myapplication E/AndroidRuntime:
FATAL EXCEPTION: main: com.example.jhon.myapplication, PID: 30286
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1.onClick(Info1.java:79)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
在 xml 代码中,我尝试将每个片段放在线性布局中,以便能够更轻松地修改它们的权重。
<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"
android:orientation="vertical"
tools:context="com.example.jhon.myapplication.Informacion">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/LinearInfo1">
<fragment
android:id="@+id/fragment"
android:name="com.example.jhon.myapplication.Info1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/LinearInfo2"
android:layout_weight="1">
<fragment
android:id="@+id/fragment2"
android:name="com.example.jhon.myapplication.Info2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_info2" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
新Logcat信息:
10-10 21:06:03.232 10887-10887/com.example.jhon.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jhon.myapplication, PID: 10887
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
at com.example.jhon.myapplication.Info1.onClick(Info1.java:80)
at android.view.View.performClick(View.java:5612)
at android.view.View$PerformClick.run(View.java:22285)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
尝试将 LinearLayout 引入内部匿名方法之外,并将相对布局参数切换为线性布局参数:
View v = inflater.inflate(R.layout.fragment_info1, container, false);
LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
XX.setLayoutParams(param);
}
});
return v;
在您的代码中试试这个。
private LinearLayout XX;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_info1, container, false);
// add here
XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param.weight = 1;
// edited here
if (XX != null) {
XX.setLayoutParams(param);
}
}
});
return v;
}
- 将 LinearLayout
设为全局变量 .
- 使 LinearLayout
最终
final LinearLayout XX = (LinearLayout) v.findViewById(R.id.LinearInfo1);
Find the corresponding ID of
LinearLayout