在我的片段中,如果未填写某个文本字段,则会以对话框的形式向用户显示错误消息,该对话框显示为已显示。 但是,我要求对话框的高度以编程方式更改,具体取决于错误消息的长度。
在我的片段的onCreateView()方法中,我已经成功地从Dialog的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:尝试在空对象引用上写入字段'int android.view.ViewGroup $ LayoutParams.height'
尝试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);
}