popupwindow

在这里插入图片描述
PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间。
1)构造方法:public PopupWindow (Context context):context上下文对象
(2)必须设置的3大要素:
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
(3)显示窗体:
a。显示在某个指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);//xoff和yoff都是偏移量
b。指定父视图,显示在父控件的某个位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);
//gravity可以是Gravity.TOP、Gravity.BOTTOM、Gravity.LEFT、Gravity.RIGHT
在这里插入图片描述
//布局



<ImageView
android:layout_alignParentRight=“true”
android:layout_marginRight=“50dp”

        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
    <ImageView
        android:id="@+id/add"
        android:layout_alignParentRight="true"
       android:src="@mipmap/qwe"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</RelativeLayout>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="搜索"/>

<ListView
    android:id="@+id/select_dialog_listview"
    android:layout_weight="8"
    android:layout_width="match_parent"
    android:layout_height="0dp"></ListView>

<RadioGroup
    android:id="@+id/rg1"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/rb1"
        android:textAlignment="center"

        android:button="@null"
        android:textSize="20sp"
        android:textColor="@color/color"
        android:text="微信"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/rb2"
        android:textAlignment="center"
        android:button="@null"
        android:textSize="20sp"

        android:text="群聊"
        android:textColor="@color/color"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />
    <RadioButton
        android:id="@+id/rb3"
        android:textAlignment="center"
        android:button="@null"
        android:textSize="20sp"
        android:text="朋友圈"
        android:textColor="@color/color"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent" />
    <RadioButton
        android:id="@+id/rb4"
        android:textAlignment="center"

        android:button="@null"
        android:textSize="20sp"
        android:text="我的"

        android:layout_weight="1"
        android:layout_width="0dp"
        android:textColor="@color/color"
        android:layout_height="match_parent" />
</RadioGroup>

//java
ListView select_dialog_listview;
ImageView add;
RadioButton rb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
select_dialog_listview=findViewById(R.id.select_dialog_listview);
rb2=findViewById(R.id.rb2);
rb2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,Main4Activity.class);
startActivity(intent);
}
});

    add=findViewById(R.id.add);
    new MyTask().execute("https://gitee.com/little_bird_oh_777/test_data_collection/raw/master/test42018061010.json");
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupWindow popupWindow = new PopupWindow(MainActivity.this);

            View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.item2,null);
            popupWindow.setContentView(view);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setWidth(600);
           popupWindow.setAnimationStyle(R.style.popuowindow);
            popupWindow.setOutsideTouchable(true);

            popupWindow.showAsDropDown(add,-100,-100);

        }

    });




}
class MyTask extends AsyncTask<String,Void,String>{
    StringBuffer stringBuffer=new StringBuffer();
    @Override
    protected String doInBackground(String... strings) {
        try {
            URL url=new URL(strings[0]);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            if (connection.getResponseCode()==200){
                InputStream inputStream=connection.getInputStream();
                byte[] bytes=new byte[1024];
                int len=0;
                while ((len=inputStream.read(bytes))!=-1){
                    String string=new String(bytes,0,len);
                    stringBuffer.append(string);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson=new Gson();
        Bean1 bean1 = gson.fromJson(s, Bean1.class);
        Apter apter=new Apter(MainActivity.this,bean1.getItem());
        select_dialog_listview.setAdapter(apter);
    }
}

}
在这里插入图片描述
在这里插入图片描述



ImageView cbu1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
cbu1=findViewById(R.id.cbu1);
cbu1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupWindow popupWindow=new PopupWindow(Main2Activity.this);
View view=getLayoutInflater().inflate(R.layout.item3,null);
popupWindow.setContentView(view);
popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setHeight(600);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.popuowindow);
popupWindow.showAtLocation(cbu1, Gravity.BOTTOM,0,0);
}
});
}
}
//效果图
在这里插入图片描述
//在anim文件夹下定义进场动画
translate
android:duration=“2000”
android:fromYDelta=“0”
android:toYDelta=“100%” />

setAnimationStyle(R.style.MyPopupWindow);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值