Android 위 챗 오른쪽 상단 드 롭 다운 대화 상자 구현

우 리 는 위 챗 을 사용 하여 오른쪽 상단 에 드 롭 다운 대화 상자 가 있다 는 것 을 알 고 있 습 니 다.우 리 는 친 구 를 추가 하고 스 캔 하 는 등 기능 을 수행 할 수 있 습 니 다.오늘 우 리 는 이 기능 을 모방 하여 실현 하 겠 습 니 다(실현 하 는 방식 은 여러 가지 가 있 습 니 다.저 는 오늘 투명 한 주제 인 Activity 를 통 해 이 루어 진 방식 만 말 하 겠 습 니 다.관심 이 있 으 면가짜 타 오 바 오 바닥 탐색 표시 줄로 옮 길 수도 있다.이 편의 실현 효 과 는 다음 과 같다.

다음은 실현 의 사고(중요)를 말한다.
첫 번 째 단계:팝 업 대화 상자 레이아웃 만 들 기

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" >
 
 <RelativeLayout
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_marginTop="45dp"
  android:layout_marginRight="20dp">
 
  <LinearLayout
   android:id="@+id/id_pop_dialog_layout"
   android:layout_width="@dimen/pop_list_width"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_alignParentTop="true"
   android:background="@drawable/pop_item_normal"
   android:orientation="vertical" >
 
   <LinearLayout
    android:id="@+id/upload_record_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView1"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/upload_icon_record" />
 
    <TextView
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/uploadRecord"
      android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
   <LinearLayout
    android:id="@+id/register_record_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView2"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/register_icon_record" />
 
    <TextView
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/registerRecord"
      android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView7"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
   <LinearLayout
    android:id="@+id/new_massage_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/pop_list_selector" >
 
    <ImageView
     android:id="@+id/id_imageView3"
     android:layout_width="@dimen/pop_dialog_icon_size"
     android:layout_height="@dimen/pop_dialog_icon_size"
     android:layout_gravity="center_vertical"
     android:layout_marginLeft="8dp"
     android:src="@drawable/message_icon_tip" />
 
    <TextView
     android:id="@+id/new_message"
     android:layout_width="@dimen/pop_list_width"
     android:layout_height="wrap_content"
     android:padding="8dp"
     android:text="@string/defaultMessage"
     android:layout_gravity="center_vertical"
     android:textColor="#fff"
     android:textSize="16sp" />
   </LinearLayout>
 
   <ImageView
    android:id="@+id/id_imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pop_line" />
 
  </LinearLayout>
 </RelativeLayout>
 
</RelativeLayout>
두 번 째 단계:이 대화 상자 의 레이아웃 을 표시 하 는 데 사용 할 액 티 비 티 를 만 듭 니 다.

package com.hfut.popdialogtest;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
 
/**
 * @author why
 * @date 2018-10-3
 */
public class MyDialogActivity extends Activity implements OnClickListener{
 
 private LinearLayout uploadRecord;
 private LinearLayout registerRecord;
 private LinearLayout newMessage;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.pop_dialog);
 
  if(getActionBar()!=null){
   getActionBar().hide();
  }
  CommonTools.setNavbarVisibility(this);
  initView();
 }
 
 
 private void initView(){
  uploadRecord = findViewById(R.id.upload_record_layout);
  registerRecord = findViewById(R.id.register_record_layout);
  newMessage = findViewById(R.id.new_massage_layout);
 
  uploadRecord.setOnClickListener(this);
  registerRecord.setOnClickListener(this);
  newMessage.setOnClickListener(this);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent event){
  finish();
  return true;
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()){
   case R.id.upload_record_layout:
   SharedData.resultID=1;
   break;
   case R.id.register_record_layout:
   SharedData.resultID=2;
   break;
   case R.id.new_massage_layout:
   SharedData.resultID=3;
   break;
   default:
   SharedData.resultID=0;
   break;
  }
  this.finish();
 }
}
세 번 째 단계:메 인 인터페이스 만 들 기
MainActivity.java 코드:

package com.hfut.popdialogtest;
 
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
 
/**
 * @author why
 * @date 2018-10-3 9:35:35
 */
public class MainActivity extends AppCompatActivity {
 
 TextView resultShow;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  resultShow = findViewById(R.id.show_choosen_result);
 
  if(getActionBar()!=null){
   getActionBar().hide();
  }
  CommonTools.setNavbarVisibility(this);
 }
 
 
 @Override
 protected void onResume() {
  switch (SharedData.resultID) {
   case 0:
    resultShow.setText("    ");
    break;
   case 1:
    resultShow.setText(getResources().getString(R.string.uploadRecord));
    break;
   case 2:
    resultShow.setText(getResources().getString(R.string.registerRecord));
    break;
   case 3:
    resultShow.setText(getResources().getString(R.string.defaultMessage));
    break;
   default:
    resultShow.setText("    ");
    break;
 
  }
  super.onResume();
 }
 
 public void openPopDialog(View view) {
  Intent intent = new Intent(this, PopDialogActivity.class);
  startActivity(intent);
 }
}
activity_main.xml 코드:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.hfut.popdialogtest.MainActivity">
 
 <ImageView
  android:onClick="openPopDialog"
  android:id="@+id/pop_dialog_icon"
  app:layout_constraintRight_toRightOf="parent"
  android:layout_marginRight="10dp"
  app:layout_constraintTop_toTopOf="parent"
  android:layout_marginTop="5dp"
  android:background="@drawable/message_tip"
  android:layout_width="50dp"
  android:layout_height="50dp" />
 
 <TextView
  android:gravity="center"
  android:textColor="@color/colorAccent"
  android:textSize="30sp"
  android:id="@+id/show_choosen_result"
  app:layout_constraintTop_toBottomOf="@id/pop_dialog_icon"
  android:layout_marginTop="50dp"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />
 
</android.support.constraint.ConstraintLayout>
네 번 째 단계:대화 상자 설정 Activity 테 마 를 투명 테마 로 설정 합 니 다.
AndroidManifest.xml 파일 코드:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.hfut.popdialogtest">
 
 <application
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:supportsRtl="true"
  android:theme="@style/AppTheme">
  <activity android:name=".MainActivity">
   <intent-filter>
    <action android:name="android.intent.action.MAIN" />
 
    <category android:name="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
  <activity android:name=".MyDialogActivity" android:theme="@android:style/Theme.Translucent" />
 </application>
 
</manifest>
STEP 5:기타 보조 코드
CommonTools.java 코드:

package com.hfut.popdialogtest;
 
import android.app.Activity;
import android.view.View;
 
/**
 * author:why
 * created on: 2018/9/11 13:34
 * description:
 */
public class CommonTools {
 
 /**
  * to controll the visibility of the Activity's navigator bar
  * @param activity
  */
 public static void setNavbarVisibility(Activity activity) {
  View decorView = activity.getWindow().getDecorView();
  decorView.setSystemUiVisibility(
    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
      | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
      | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
      | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
      | View.SYSTEM_UI_FLAG_FULLSCREEN
      | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
 }
 
}
Values 디 렉 터 리 에 있 는 dimens.xml 코드:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <dimen name="pop_list_width">160dp</dimen>
 <dimen name="pop_dialog_icon_size">60dp</dimen>
 <dimen name="pop_dialog_icon_tip_size">40dp</dimen>
</resources>
Values 디 렉 터 리 에 있 는 strings.xml 코드:

<resources>
 <string name="app_name">             </string>
 
 <string name="uploadRecord">    </string>
 <string name="registerRecord">    </string>
 <string name="defaultMessage">    </string>
 
</resources>
다른 자원 파일 은 추가 하지 않 습 니 다.우리 가 정리 해 보 자.사실은 이런 절차 다.
  • 주 Activity 의 팝 업 창 대화 상자 아이콘 을 클릭 하여 투명 한 Acitivity
  • 를 엽 니 다.
  • 새로운 Activity 에서 논리 적 처 리 를 하고 결 과 를 주 Activity 가 접근 할 수 있 는 데이터 필드 에 저장 한 다음 finish 자신
  • 주 Activity 는 다시 상호작용 을 할 수 있 고 onResume 에서 처리 결과 에 대한 분석 과 처 리 를 실현 한다.예 를 들 어 주 Activity UI 를 수정 하 는 것 이다. 
  • 이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

    좋은 웹페이지 즐겨찾기