我想在高度爲h/3和寬度爲3w/5的佈局中顯示絕對中心的矩形框(w:屏幕寬度,h:屏幕高度)。請幫助我找到一個解決方案,在此先感謝。在佈局中顯示絕對中心的矩形框
5
A
回答
3
你可以使用線性佈局使用重量來調整它 我已經粘貼了一個示例代碼,下面希望這有助於。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/transparent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
/>
<TextView
android:id="@+id/desiredbox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:layout_gravity="center"
android:background="@color/skyblueBackground"
android:layout_weight="1"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="5"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
2
創建一個自定義視圖,該視圖擴展了View
類並覆蓋了onDraw()
方法以創建所需的矩形。你可以參考:Android canvas draw rectangle來獲得大致的想法。
如果你的問題是:如何放置在容器內視圖 - 在父視圖的構造函數中添加此:
final ViewTreeObserver vto = this.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// here, the dimensions of the parent are available
int containerHeight = instance.getHeight();
int containerWidth = instance.getWidth();
childView.setY(containerHeight/3);
childView.setX(containerWidth * 3/5);
instance.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
其中instance
是你的容器視圖的參考。
3
是的。這可以使用一個相對佈局父母和另一個佈局(你的盒子)作爲中心。而你的盒子的寬度和高度可以在java中提到,而不是在xml中提到。
相關問題
- 1. 在相對佈局中使用矩形堆棧顯示金字塔形狀
- 2. 桌布佈局的矩形
- 3. 在LineChart中顯示矩形
- 4. 本地腳本中絕對佈局中心的位置元素
- 5. 在Android佈局中絕對居中
- 6. 中心在Java框架/面板中對齊矩形
- 7. Xamarin形式絕對佈局而不是相對佈局
- 8. 在Android上的佈局中顯示對話框
- 9. 未在中心顯示jQuery對話框
- 10. 矩形邊框不顯示
- 11. Android對齊佈局中心
- 12. 算法佈局矩形窗口在2D顯示
- 13. 顯示矩形框在Android的
- 14. Flex中的絕對和相對佈局
- 15. 對話框佈局不正確顯示
- 16. 如何在視圖框內顯示一半的矩形畫布
- 17. 移動畫布矩形觸摸問題(矩形中心)
- 18. 在DraggableGridView中顯示佈局
- 19. 佈局 - 中心等佈局
- 20. 在JScrollPane中使用絕對佈局
- 21. 中心的GridView中相對佈局
- 22. 在對話框中的相對佈局的Android佈局
- 23. QML中未顯示矩形
- 24. 顯示畫布矩形的TextView
- 25. android絕對佈局
- 26. Eclipse - 在Android視圖的圖形佈局中顯示佈局指南(線條)
- 27. 如何在佈局中顯示畫布?
- 28. 在框架佈局中顯示imageview背後的進度對話框
- 29. 如何使用`image`在常規佈局中顯示矩陣?
- 30. 顯示文本框居中在一個傾斜的矩形