【问题标题】:How to set popup window style for responsive UI in android如何在android中为响应式UI设置弹出窗口样式
【发布时间】:2017-05-18 10:57:40
【问题描述】:

在我的 android 应用程序中,我使用了弹出窗口。 关于弹出窗口有一个问题。 在更大的设备(高度或宽度)中,它会清楚地显示并自动设置左边或右边的边距。 在像 nexus one 这样的小型设备中,弹出窗口会粘在未设置左侧或右侧边距的设备上。

或者另一个问题是在棒棒糖或棉花糖中有一个按钮看起来像浮动按钮。您可以在我附在下面的屏幕截图中查看。

这个按钮在棉花糖或更高版本中看起来不错。 但是在 kitkat 中只有一个看起来像一个简单箭头的方法来解决它。

这里我指定了我的弹出窗口源代码或 kitkat 设备 UI 和 Marshmallow UI 的屏幕截图。

请任何人告诉我如何解决它。提前感谢您的支持。

raw_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="horizontal"
android:padding="16dp">

<TextView
    android:id="@+id/txtMainHeading"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="10dp"
    android:text="@string/headertext"
    android:textColor="@color/colorBlack"
    android:textSize="16sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/txtText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:padding="10dp"
    android:text="@string/popupsubtext"
    android:textColor="@color/colorBlack" />

<View
    android:id="@+id/viewMain"
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:layout_below="@+id/txtText"
    android:background="@color/bg_border" />


<View
    android:id="@+id/view"
    android:layout_width="wrap_content"
    android:layout_height="2dp"
    android:layout_below="@+id/LayoutDetail"
    android:layout_marginLeft="95dp"
    android:layout_marginRight="10dp"
    android:background="@color/bg_border" />

<LinearLayout
    android:id="@+id/LayoutDetail1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/view"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtLastNameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/puplastname"
        android:textColor="@color/colorBlack"
        android:textSize="15sp" />


    <TextView
        android:id="@+id/txtLastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:focusableInTouchMode="true"
        android:text="@string/txtLastnametext"
        android:textColor="@color/button_text_dialog"
        android:textSize="16sp" />
</LinearLayout>

<View
    android:id="@+id/view1"
    android:layout_width="wrap_content"
    android:layout_height="2dp"
    android:layout_below="@+id/LayoutDetail1"
    android:layout_marginLeft="95dp"
    android:layout_marginRight="10dp"
    android:background="@color/bg_border" />

<LinearLayout
    android:id="@+id/LayoutDetail2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/view1"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtEmailText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/pupEmail"
        android:textColor="@color/colorBlack"
        android:textSize="15sp" />


    <TextView
        android:id="@+id/txtEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/txtemailtext"
        android:textColor="@color/button_text_dialog"
        android:textSize="16sp" />


</LinearLayout>

<View
    android:id="@+id/view2"
    android:layout_width="wrap_content"
    android:layout_height="2dp"
    android:layout_below="@+id/LayoutDetail2"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="70dp"
    android:layout_marginRight="10dp"
    android:background="@color/bg_border" />

<ImageView
    android:id="@+id/btnNext"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/LayoutDetail2"
    android:layout_margin="@dimen/fab_margin"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:layout_marginTop="14dp"
    android:background="@drawable/shape_oval"
    android:elevation="2dp"
    android:scaleType="center"
    android:src="@drawable/ic_icon_right_1" />

<LinearLayout
    android:id="@+id/LayoutDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/txtText"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/txtFirstNameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="@string/pupfirstname"
        android:textColor="@color/colorBlack"
        android:textSize="15sp" />


    <TextView
        android:id="@+id/txtFirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:focusableInTouchMode="true"
        android:text="@string/txtFirstnametext"
        android:textColor="@color/button_text_dialog"
        android:textSize="16sp" />

</LinearLayout>

弹出窗口.java

private Context mContext;
private Activity mActivity;
private android.widget.PopupWindow mPopupWindow;
private RelativeLayout mRelativeLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_countrycode);


    mContext = getApplicationContext();


    // Get the activity
    mActivity = CountrycodeActivity.this;

    // Get the widgets reference from XML layout
    mRelativeLayout = (RelativeLayout) findViewById(R.id.mRelativeLayout);

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

    // Inflate the custom layout/view
    View customView = inflater.inflate(R.layout.raw_layout, null);

    mPopupWindow = new android.widget.PopupWindow(
            customView,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
    );
    //    mPopupWindow.setContentView(findViewById(R.id.activity_view_pager));
    mPopupWindow.setAnimationStyle(R.style.PopupAnimation);

    if (Build.VERSION.SDK_INT >= 21) {
        mPopupWindow.setElevation(24f);
    }

    new Handler().postDelayed(new Runnable() {

        public void run() {
            mPopupWindow.showAtLocation(mRelativeLayout, Gravity.CENTER, 0, 0);
        }

    }, 100L);

}

@Override
protected void onStop() {
    super.onStop();
    mPopupWindow.dismiss();
}

界面截图

In marshmallow as well as bigger device nexus 5.1

In nexus one as well as kitkat device or smaller

【问题讨论】:

  • 请同时提供您的xml
  • 请查看我编辑的帖子@SunishaSindhu
  • 为相对布局留出余量
  • 不工作:( @SunishaSindhu

标签: android xml styles popupwindow


【解决方案1】:
android:layout_width="fill_parent"

设置RelativeLayout宽度属性fill_parent

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 2011-09-14
    • 2012-10-27
    • 1970-01-01
    相关资源
    最近更新 更多