Android 에서 아래쪽 에서 팝 업 되 는 Dialog 예제 구현(1)

7155 단어 dialog하단 팝 업
개술
먼저 효과 도 를 보 여 드 리 겠 습 니 다.

중간 에 있 는 디 스 플레이 탄 상자 단 추 를 누 르 면 아래쪽 에서 대화 상 자 를 꺼 냅 니 다.사용 자 는 사진 을 찍 거나 앨범 에서 해당 하 는 동작 을 선택 할 수 있 습 니 다.다음은 어떻게 실현 되 는 지 보 겠 습 니 다.
코드 구현
홈 페이지 레이아웃 파일,간단 합 니 다.단 추 를 누 르 면 이벤트 에 응답 합 니 다.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:fitsSystemWindows="true"
  tools:context="com.example.dialogdemo.MainActivity">
    <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:onClick="show"
      android:text="    "
      />
</RelativeLayout>
다음은 대화 상자 의 레이아웃 을 봅 니 다:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:orientation="vertical"
  android:background="@drawable/background"
  android:layout_height="match_parent">
  <TextView
    android:id="@+id/takePhoto"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_margin="2dp"
    android:gravity="center"
    android:text="  "
    android:textColor="#0000ff"
    android:textSize="18sp"
    android:textStyle="bold" />
  <View
    android:layout_width="match_parent"
    android:layout_height="1px"
    android:background="#9e9e9e"
    />
  <TextView
    android:id="@+id/choosePhoto"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_margin="2dp"
    android:gravity="center"
    android:text="     "
    android:textColor="#0000ff"
    android:textSize="18sp"
    android:textStyle="bold" />
</LinearLayout>

루트 레이아웃 은 수직 선형 레이아웃 으로 배경 을 하나 더 했 습 니 다.흰색 사각형,네 개의 각 호 도 는 5dp 이 고 코드 는 다음 과 같 습 니 다.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#ffffff"/>
  <corners android:radius="5dp"/>
</shape>

선형 레이아웃 에는 두 개의 TextView 와 한 개의 횡선 이 있 습 니 다.간단 하 다
다음은 자바 코드:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  private View inflate;
  private TextView choosePhoto;
  private TextView takePhoto;
  private Dialog dialog;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
  public void show(View view){
    dialog = new Dialog(this,R.style.ActionSheetDialogStyle);
    //        
    inflate = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
    //     
    choosePhoto = (TextView) inflate.findViewById(R.id.choosePhoto);
    takePhoto = (TextView) inflate.findViewById(R.id.takePhoto);
    choosePhoto.setOnClickListener(this);
    takePhoto.setOnClickListener(this);
    //      Dialog
    dialog.setContentView(inflate);
    //    Activity     
    Window dialogWindow = dialog.getWindow();
    //  Dialog       
    dialogWindow.setGravity( Gravity.BOTTOM);
    //       
    WindowManager.LayoutParams lp = dialogWindow.getAttributes();
    lp.y = 20;//  Dialog       
//            
    dialogWindow.setAttributes(lp);
    dialog.show();//     
  }

  @Override
  public void onClick(View view) {
    switch (view.getId()){
      case R.id.takePhoto:
        Toast.makeText(this,"     ",Toast.LENGTH_SHORT).show();
        break;
      case R.id.choosePhoto:
        Toast.makeText(this,"        ",Toast.LENGTH_SHORT).show();
        break;
    }
    dialog.dismiss();
  }
}

창 스타일:

 <style name="ActionSheetDialogStyle" parent="@android:style/Theme.Dialog">

    <!--      -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <!--   Activity   -->
    <item name="android:windowIsFloating">true</item>
    <!--    -->
    <item name="android:windowFrame">@null</item>
    <!-- Dialog          -->
    <item name="android:backgroundDimEnabled">true</item>
    <!--     -->
    <item name="android:windowNoTitle">true</item>
    <!--     -->
    <item name="android:windowIsTranslucent">true</item>
    <!-- Dialog        -->
    <item name="android:windowAnimationStyle">@style/ActionSheetDialogAnimation</item>
  </style>
  <!-- ActionSheet     -->
  <style name="ActionSheetDialogAnimation" parent="@android:style/Animation.Dialog">
    <item name="android:windowEnterAnimation">@anim/actionsheet_dialog_in</item>
    <item name="android:windowExitAnimation">@anim/actionsheet_dialog_out</item>
  </style>

대화 상자 에 애니메이션 코드 가 나타 납 니 다:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="200"
  android:fromYDelta="100%"
  android:toYDelta="0" />
대화 상자 에서 사라 진 코드:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="200"
  android:fromYDelta="0"
  android:toYDelta="100%" />
총화
이번에 실 현 된 Dialog 는 주로 TextView 를 통 해 이 루어 졌 으 며,상태 선택 기 및 취소 단 추 를 추가 하지 않 았 으 며,다음 글 에 서 는 대화 상자 의 표현 형식 을 조금 변경 할 것 입 니 다.프로젝트 의 개발 수요 에 적응 하 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기