search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Android] 元件置頂

先新增兩個imageView元件選擇不同的圖片並且把他們疊在一起

一般沒經過設定的情況下,元件顯示的前後方向主要是看ComPonent Tree裡面的排列

較下方排序會讓元件排列在前,但此方法在複雜的UI介面下絕對是不可靠的,所以在此示範簡易的元件置頂

所以今天會用到bringTofront()來控制元件的順序

先用ig2元件壓住ig元件,再藉由控制元件的Code讓ig元件顯示在前面

示範

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=".MainActivity">

    <ImageView
        android:id="@+id/ig"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ImageView
        android:id="@+id/ig2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        app:srcCompat="@mipmap/ic_launcher_round" />

android.support.constraint.ConstraintLayout>

JAVA

public class MainActivity extends AppCompatActivity {
ImageView ig,ig2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ig =(ImageView)findViewById(R.id.ig);
        ig2 =(ImageView)findViewById(R.id.ig2);
        ig.bringToFront();
    }
}

相關文章:

[Android] Android 程式設計教學

 



熱門推薦

本文由 kk665403pixnetnetblog 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦