我想在高度爲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. 在Android佈局中絕對居中
- 3. 在Android上的佈局中顯示對話框
- 4. Xamarin形式絕對佈局而不是相對佈局
- 5. 未在中心顯示jQuery對話框
- 6. 如何在視圖框內顯示一半的矩形畫布
- 7. 在DraggableGridView中顯示佈局
- 8. 移動畫布矩形觸摸問題(矩形中心)
- 9. 在JScrollPane中使用絕對佈局
- 10. QML中未顯示矩形
- 11. Eclipse - 在Android視圖的圖形佈局中顯示佈局指南(線條)
- 12. 如何使用`image`在常規佈局中顯示矩陣?
- 13. 在框架佈局中顯示imageview背後的進度對話框
- 14. 顯示文本框居中在一個傾斜的矩形
- 15. 相對於佈局的中心文本
- 16. Android的相對佈局中心每
- 17. 在Eclipse中顯示佈局指南線圖形佈局編輯器
- 18. Realtive佈局與絕對佈局
- 19. 中心佈局Android
- 20. 在佈局中顯示無效的HTML
- 21. 完全絕對定位佈局顯示空白
- 22. 顯示:框不會在Firefox絕對
- 23. 如何在Eclipse中以圖形方式顯示佈局?
- 24. Android:在圖形佈局工具中顯示自定義字體
- 25. 在佈局中的Android中心textview
- 26. 中心對準柔性佈局
- 27. 顯示矩形中的原始圖像
- 28. 的Android - 中心佈局
- 29. 的iOS coredata圖形模型佈局,顯示Ø對方
- 30. 非矩形的android自定義佈局