android 透明彈出搜尋方塊

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   ar   color   os   

1.在QQ一些APP當中有是彈出一個半透明的搜尋方塊的,其實這種效果就是很多種方法,自訂一個Dialog,或者直接將activity的背景變成半透明的也可以的。

下面就是將activity變成半透明的方法。如下:

2.代碼如下:

在styles.xml檔案中添加自訂Theme

1     <style name="MyTranspaent">2         3         <item name="android:windowBackground">@color/transparent_background</item>    4         <item name="android:windowNoTitle">true</item>   5         <item name="android:windowIsTranslucent">true</item>   6         <item name="android:windowAnimationStyle">@+android:style/Animation.Translucent</item>  7     </style>  

搜尋介面的Activity:

 1 public class SecondActivity extends Activity { 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_second); 7     } 8  9     @Override10     public boolean onCreateOptionsMenu(Menu menu) {11         // Inflate the menu; this adds items to the action bar if it is present.12         getMenuInflater().inflate(R.menu.second, menu);13         return true;14     }15 16     @Override17     public boolean onTouchEvent(MotionEvent event) {18 19         switch (event.getAction()) {20         case MotionEvent.ACTION_DOWN:21             SecondActivity.this.finish();22             break;23 24         default:25             break;26         }27 28         return super.onTouchEvent(event);29     }30 31 }

activity_second.xml檔案:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="horizontal" 6     tools:context=".SecondActivity" > 7  8     <EditText 9         android:id="@+id/ed"10         android:layout_width="wrap_content"11         android:layout_height="wrap_content"12         android:layout_weight="1.0"13         android:background="#F8F8FF"14         android:drawableLeft="@drawable/ic_search_normal"15         android:hint="請輸入要查詢內容"16         android:textColor="#000033" />17 18 </LinearLayout>

主介面裡面就是一個button來彈出這個activity

 1 public class MainActivity extends Activity { 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_main); 7  8         findViewById(R.id.Button01).setOnClickListener(new OnClickListener() { 9 10             @Override11             public void onClick(View v) {12                 Intent i = new Intent();13                 i.setClass(MainActivity.this, SecondActivity.class);14                 startActivity(i);15             }16         });17 18     }19 20 }

關鍵的就是要在Manifest.xml檔案中給彈出的activity添加theme

1   <activity2             android:name="com.example.compass.SecondActivity"3             android:label="@string/title_activity_second"4             android:theme="@style/MyTranspaent" >5   </activity>

源碼下載

android 透明彈出搜尋方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.