我想在一个高度为h/3,宽度为3w/5(w:屏幕宽度,h:屏幕高度)的布局中,在绝对中心显示一个矩形框。请帮我找到解决办法,提前谢谢。

最佳答案

可以使用“权重”使用线性布局调整它
我在下面粘贴了一个示例代码,希望对您有所帮助。

<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"
/>

07-24 09:45
查看更多