安卓的LinearLayout实现按钮的点击效果

1.先在drawable文件夹下面创建控制背景的xml文件
选中的背景色

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid
        android:color="#33c0c0c0" />

</shape>

未选中的背景色

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid
        android:color="#ffffff"
        />

</shape>

2.先在drawable文件夹下面创建背景选择器的xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!--     <item android:drawable="@drawable/pressed" android:state_selected="true"  android:state_pressed="true"></item>
    <item android:drawable="@drawable/pressed" android:state_checked="true"></item>
 -->    
    <item android:drawable="@drawable/pressed" android:state_pressed="true"></item>
 <item android:drawable="@drawable/unpressed" android:state_pressed="false" android:state_selected="false"></item>

</selector>

3.在LinearLayout控件中
通过设置背景属性来调用选择器文件:android:background=”@drawable/back_down”
但是在LinearLayout控件中必须添加上 android:clickable=”true”

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/back_down"
                    android:clickable="true"
                    android:orientation="horizontal"
                    android:paddingBottom="8dp"
                    android:paddingLeft="15dp"
                    android:paddingRight="15dp"
                    android:paddingTop="8dp" >
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用程序中实现弹窗,可以按照以下步骤操作: 1. 在布局文件中添加一个按钮和一个布局文件,用于显示弹窗内容。 2. 在Java代码中,获取按钮和弹窗布局文件的实例,并设置按钮点击事件。 3. 在按钮点击事件中,创建一个弹窗实例,并设置弹窗的内容和样式。 4. 显示弹窗。 以下是一个简单的示例代码(假设我们的布局文件名为activity_main.xml,弹窗布局文件名为popup_window.xml): 1. 在activity_main.xml中添加一个按钮和一个布局文件: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btn_open_popup" android:text="Open Popup" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:id="@+id/popup_layout" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:background="#ffffff"> <TextView android:text="This is a popup window" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_close_popup" android:text="Close" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout> ``` 2. 在MainActivity.java中,获取按钮和弹窗布局文件的实例,并设置按钮点击事件: ``` public class MainActivity extends AppCompatActivity { private Button btnOpenPopup; private LinearLayout popupLayout; private PopupWindow popupWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnOpenPopup = findViewById(R.id.btn_open_popup); popupLayout = findViewById(R.id.popup_layout); btnOpenPopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openPopupWindow(); } }); } // TODO: 实现openPopupWindow()方法 } ``` 3. 在openPopupWindow()方法中,创建一个弹窗实例,并设置弹窗的内容和样式: ``` private void openPopupWindow() { popupWindow = new PopupWindow(this); popupWindow.setContentView(popupLayout); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); Button btnClosePopup = popupLayout.findViewById(R.id.btn_close_popup); btnClosePopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } ``` 4. 最后,在openPopupWindow()方法中,显示弹窗: ``` private void openPopupWindow() { popupWindow = new PopupWindow(this); popupWindow.setContentView(popupLayout); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); Button btnClosePopup = popupLayout.findViewById(R.id.btn_close_popup); btnClosePopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); popupWindow.showAtLocation(btnOpenPopup, Gravity.CENTER, 0, 0); } ``` 现在,当用户点击按钮时,弹窗将显示在屏幕中央。用户可以点击弹窗中的“关闭”按钮来关闭弹窗。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值