在Android 4.1中定位dialogfragment有问题,在其他版本的Android OS上一切正常,问题只出现在4.1中。 所以我要做的是在屏幕底部定位片段,但在Android 4.1中它看起来像这样的空白:
我的代码:
public class ContactsDialog extends DialogFragment {
String contactNumber = null;
String skypeContact = null;
String userID = null;
int flatsCount = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView contactTV, skypeTV;
View layout = inflater.inflate(R.layout.contacts_dialog, container);
contactTV = (TextView) layout.findViewById(R.id.contactNumber);
skypeTV = (TextView) layout.findViewById(R.id.skypeName);
RelativeLayout skypeContactsLayout = (RelativeLayout) layout.findViewById(R.id.skypeContactsLayout);
RelativeLayout contactsLayout = (RelativeLayout) layout.findViewById(R.id.contactsLayout);
RelativeLayout allUserFlats = (RelativeLayout) layout.findViewById(R.id.allUserFlats);
contactNumber = getArguments().getString("contactNumber", null);
skypeContact = getArguments().getString("skypeName", null);
userID = getArguments().getString("userID", null);
flatsCount = getArguments().getInt("flatsCount");
if(TextUtils.isEmpty(skypeContact)) {
skypeContactsLayout.setVisibility(View.GONE);
}
if (TextUtils.isEmpty(userID)) {
allUserFlats.setVisibility(View.GONE);
}
if (flatsCount == 1) {
allUserFlats.setVisibility(View.GONE);
}
String convertedNumber = convertNumber(contactNumber);
contactTV.setText(convertedNumber);
skypeTV.setText(skypeContact);
WindowManager.LayoutParams params = getDialog().getWindow()
.getAttributes();
params.gravity = Gravity.BOTTOM | Gravity.CENTER;
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setAttributes(params);
contactsLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + contactNumber));
startActivity(intent);
}
});
allUserFlats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("userID", userID);
getActivity().finish();
startActivity(intent);
}
});
skypeContactsLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isSkypeClientInstalled(getActivity())){
Intent sky = new Intent("android.intent.action.VIEW");
sky.setData(Uri.parse("skype:" + skypeContact));
startActivity(sky);
} else {
Toast.makeText(getActivity(), "Skype не установлен", Toast.LENGTH_LONG).show();
}
}
});
return layout;
}
public boolean isSkypeClientInstalled(Context myContext) {
PackageManager myPackageMgr = myContext.getPackageManager();
try {
myPackageMgr.getPackageInfo("com.skype.raider", PackageManager.GET_ACTIVITIES);
}
catch (PackageManager.NameNotFoundException e) {
return (false);
}
return (true);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDialog);
}
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
getDialog().setCanceledOnTouchOutside(true);
}
private String convertNumber(String number) {
StringBuffer sb = new StringBuffer(number);
sb.insert(2," (");
sb.insert(7,") ");
sb.insert(12,"-");
sb.insert(15,"-");
return String.valueOf(sb);
}
}
我还有下一个布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="14dp"
android:background="@color/whiteTextView"
android:id="@+id/contactsLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/phone_grey1"
android:id="@+id/phoneImage" />
<TextView
android:id="@+id/contactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/blackTextView"
android:text="Все"
android:textSize="18sp"
android:layout_above="@+id/contactTV"
android:layout_alignLeft="@+id/contactTV"
android:layout_alignStart="@+id/contactTV" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/contactsString"
android:textSize="12sp"
android:textColor="@color/greyTextView"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:id="@+id/contactTV"
android:layout_alignBottom="@+id/phoneImage"
android:layout_toRightOf="@+id/phoneImage"
android:layout_toEndOf="@+id/phoneImage" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/skypeContactsLayout"
android:background="@color/whiteTextView"
android:layout_below="@+id/contactsLayout"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/skype1"
android:id="@+id/skypeImage" />
<TextView
android:id="@+id/skypeName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/blackTextView"
android:text="Все"
android:textSize="18sp"
android:layout_above="@+id/skypeTV"
android:layout_alignLeft="@+id/skypeTV"
android:layout_alignStart="@+id/skypeTV"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/skypeString"
android:textSize="12sp"
android:textColor="@color/greyTextView"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:id="@+id/skypeTV"
android:layout_alignBottom="@+id/skypeImage"
android:layout_toRightOf="@+id/skypeImage"
android:layout_toEndOf="@+id/skypeImage" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/whiteTextView"
android:paddingBottom="14dp"
android:id="@+id/allUserFlats"
android:layout_below="@+id/skypeContactsLayout"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:layout_centerVertical="true"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="16dp"
android:background="@drawable/hamburger1"
android:id="@+id/otherOffersImage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/allFlats"
android:layout_marginLeft="25dp"
android:layout_marginStart="25dp"
android:textSize="18sp"
android:textColor="@color/blackTextView"
android:id="@+id/otherOffersTV"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/otherOffersImage"
android:layout_toEndOf="@+id/otherOffersImage" />
</RelativeLayout>
</LinearLayout>
我的样式文件
<style name="CustomDialog" parent="@android:style/Theme.Holo.Light" >
<item name="android:windowIsFloating">true</item>
</style>
答案 0 :(得分:3)
首先尝试设置:
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
而不是
params.gravity = Gravity.BOTTOM | Gravity.CENTER;
另外,我认为这不是必要的:
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
因为您的样式DialogFragment.STYLE_NO_FRAME
已禁用TITLE
区域。
如果这没有帮助,请粘贴您的CustomDialog
样式。
编辑:
尝试将您的风格改为:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/white</item>
<item name="android:background">@android:color/transparent</item>
</style>