android - 在布局的绝对中心显示一个矩形框

标签 android android-layout

我想在高度为 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"
/>

关于android - 在布局的绝对中心显示一个矩形框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16184217/

相关文章:

android - 相机布局错误

java - 如何从抽屉导航的 fragment 启动 Activity

android - 如何一个一个播放音频文件

java - 如何将数据从后台线程移动到ui线程

安卓 Lollipop : ImageView ignoring fitsSystemWindows attribute (transparent status bar)

android - 如何在 Android 中给两个线性布局之间的空间

android - “main cannot be resolved or is not a field”

Android ViewAnimator/ViewFlipper/ViewSwitcher 和 setDisplayedChild 的硬编码值

android - 当我在 Activity 中并使用 finish() 方法时,如何重新加载 fragment 页面?

android - 在线性布局中对齐和包装图像按钮