android framlayout 佈局控制元件使用
阿新 • • 發佈:2019-02-10
現在又是12點了。今日比較有意義的事情就是使用到了framelayout。還是之前一樣,不嘮叨!記錄每一點收穫。
一、動機: 我想在ImageView 上面放置textview 控制元件,來顯示文字,這樣我就不需要去畫背景了。像是在偷懶。
二、辦法:從度娘那裡獲得一個android 控制元件 《Framelayout》。
使用Framelayout 的原因是:可以將控制元件層疊起來。
三、使用方法
1、在已有的xml 佈局檔案《activity_main.xml》中,放置Framelayout。方法很簡單,找到控制元件託放進去就OK了。
2、將Textview 控制元件的xml 檔案放到Framelayout 的xml 檔案中。這樣Textview 就成了Framelayout 裡面的控制元件了。
像下面:
<FrameLayout android:id="@+id/frameLayout2" android:layout_width="394dp" android:layout_height="279dp" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <ImageView /*好吧,我是拷貝進來的 */ android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="289dp" android:visibility="visible" app:srcCompat="@drawable/line" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="86dp" /> <TextView /*好吧,我是拷貝進來的 */ android:id="@+id/second_tv" android:layout_width="368dp" android:layout_height="112dp" android:layout_marginStart="10dp" android:layout_marginTop="40dp" android:fontFamily="serif" android:text="second" tools:layout_editor_absoluteX="21dp" tools:layout_editor_absoluteY="0dp" /> <TextView android:id="@+id/week_tv" android:layout_width="237dp" android:layout_height="79dp" android:layout_marginLeft="80dp" android:layout_marginTop="150dp" android:text="week" app:layout_constraintBottom_toTopOf="@+id/guideline5" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </FrameLayout>
這個FrameLayout 控制元件是我在UI 介面拖進去的,然後在activity_main.xml 檔案中就生成了<FrameLayout> </FrameLayout>結構。然後將本在主佈局檔案裡面的TextView 控制元件,拷貝到這裡面來。
3、將TextView 拷貝到FrameLayout中後,更改TextView 的位置只能使用相對於FrameLayout 的距離進行位置調整。一點一點的調位置到自己喜歡為止。
4、我是用的比較傻的辦法,距離是每一個邊挨著調節的。還有更好的辦法,希望看到這篇文章的戰友指點迷津。
甚是感謝。