在我的片段中,如果未填写某个文本字段,则会以对话框的形式向用户显示一条错误消息,该对话框显示为 itended。但是,我需要根据错误消息的长度以编程方式更改对话框的高度。
在我的片段的 onCreateView() 方法中,我已经成功地能够从对话框的 xml 文件中打印 TextView 元素之一的值。
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
val 成功打印为“TeStInG”,这是正确的值。xml文件如下,确认android:text="TeStInG"。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/signUpFillDeets"
android:orientation="vertical"
android:layout_width="210dp"
android:layout_height="230dp"
android:background="#ffffffff">
<ImageView
android:id="@+id/crossImage"
android:layout_width="match_parent"
android:src="@drawable/dialog_cross"
android:layout_height="120dp"
android:gravity="center"
android:background="#1F1C1C"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/dialogText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/crossImage"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:text="TeStInG"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:id="@+id/dialogButton"
android:layout_width="70dp"
android:layout_height="30dp"
android:text="OK"
android:textStyle="italic"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/dialogText"
android:layout_marginBottom="20dp"
android:background="@drawable/button_background_red"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
</RelativeLayout>
但是,当我尝试通过其 ID (signUpFillDeets) 访问布局文件时,不会发生任何更改。
下面的代码全部在 onCreateView() 中完成,实际显示对话框的代码在 setUserVisibleHint() 方法中。所以从逻辑上讲,如果我此时设置错误消息的高度,它应该对对话框的尺寸产生永久影响,直到应用程序关闭。
尝试1:
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
尝试2:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
导致错误 - java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.height' on an null object reference
尝试 3:
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams dimensions = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000);
layout.setLayoutParams(dimensions);
有人可以帮忙吗?
更新 - onCreateView() 和 setUserVisibleHint()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.sign_up_fragment_third, container, false);
// we need to apply our custom font to the TextViews on our fragment. So first need to reference the TextView components via our sign_up_fragment_third view, and apply the TypeFact
View title = view.findViewById(R.id.signUpTertTitle);
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
View t = deetsView.findViewById(R.id.dialogText);
String val = ((TextView)t).getText().toString();
System.out.println(val);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
/*
Attempt 1
deetsView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 3000));
Attempt 2
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
*/
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// if the user has not supplied a first name then we will display to them an prompt to enter a first name.
String firstName = ((EditText)SignUpFragmentSecond.firstName).getText().toString();
System.out.println("First name val is " + firstName);
String errorMessage = "Need to fill out ";
if(firstName.equals("")){
System.out.println("First name is empty");
errorMessage += " First Name, ";
System.out.println("Now is " + errorMessage);
}
if(!errorMessage.equals("Need to fill out ")){
SignUpActivity.pager.setCurrentItem(1);
SignUpFillDetails dialog = new SignUpFillDetails();
dialog.showDialog(getActivity(), errorMessage);
}
} else {
System.out.println("THIRD is not visible to the user");
}
}
添加了更新的 onViewCreated() 并列出了导入以防它被使用
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
@Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
LayoutInflater inflater = LayoutInflater.from(getContext());
View deetsView = inflater.inflate(R.layout.sign_up_fill_details, null);
RelativeLayout layout = (RelativeLayout)deetsView.findViewById(R.id.signUpFillDeets);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
params.height = 300;
layout.setLayoutParams(params);
}