高さ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を参照することができます。あなたの質問がある場合は
:コンテナ内のビューを配置する方法 - 親ビューのコンストラクタでこれを追加します。instance
は、あなたのコンテナビューへの参照である
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);
}
}
});
。
3
うん。これは、相対レイアウトの親と、その中心に配置された別のレイアウト(ボックス)を使用して行うことができます。あなたのボックスの幅と高さは、xmlではなくjavaで記述することができます。
関連する問題
- 1. 相対レイアウト中心垂直
- 2. Android AdViewを線形レイアウトの中心に揃える方法
- 3. 絶対的な位置付けを中心にCSSを中心にする
- 4. 絶対値の表示anglejs
- 5. テキストを含むjqueryダイアログを表示中(絶対中)
- 6. テキストアラインメントの方法:IEの絶対要素を中心にしますか?
- 7. Xcodeの「フレーム長方形」と「レイアウト長方形」
- 8. 絶対中心のテキストですが左揃え
- 9. 地図の中心にドットを表示
- 10. Androidレイアウトでの絶対サイズと相対サイズの混在 - 方法
- 11. center document NSScrollViewの中心に表示
- 12. 絶対絶対位置 - スクロールバーが表示されない
- 13. 純粋なJavaScript:本文の絶対位置divを中心に設定する
- 14. jQueryを中心にdivを表示
- 15. 相対レイアウトと線形レイアウト
- 16. 長方形をn個の小さな矩形に分割し、各中心を計算するアルゴリズム
- 17. 2つの線形レイアウト表示
- 18. CSS - 中心レイアウトでIFRAME
- 19. 絶対長単位の精度
- 20. 中心を中心に回転する矩形
- 21. 絶対配置されたdivを最大幅で中心に設定する
- 22. レイアウトパネル上のXAMLレイアウト中心のアイテム
- 23. 固定長方形の長方形パッキング
- 24. 非表示と表示のdivをクリック上の絶対位置のアニメーションで&ハイド
- 25. wpfで絶対勾配の長方形(高さが異なる)を塗りつぶす方法は?
- 26. 相対的なレイアウトで矩形のスタックを使用してピラミッドの形状を表示
- 27. 矩形を中心に回転する
- 28. 線形レイアウトの表示/非表示をスムーズにアニメーションします。android
- 29. 矩形内のJava中心のテキスト
- 30. 顔に赤い矩形のボックスが表示されない