Android 中心内部布局

发布于 2024-12-12 07:24:25 字数 4034 浏览 0 评论 0 原文

我需要一个盒子中的盒子,第一个盒子的顶部有大约 33% 的空房间,然后是另一个盒子在下面。我需要它居中。我希望它在手机上本质上是 fill_parent ,但在平板电脑(此应用程序的主要设备)上,它应该保持相同的大小,但位于屏幕中间居中。我该怎么做?

我举了一些例子,因为对我来说解释起来有点困难。

移动设备

|---------------------------|
|     ~33% Blank space      |
|                           |
|  |---------------------|  |
|  |                     |  |
|  |                     |  |
|  |      Login Box      |  |
|  |      Password       |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |---------------------|  |
|---------------------------| 

平板电脑

|---------------------------------------|
|           ~33% Blank space            |
|                                       |
|        |---------------------|        |
|        |                     |        |
|        |                     |        |
|        |      Login Box      |        |
|        |      Password       |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |---------------------|        |
|---------------------------------------| 

编辑 这是我当前拥有的 XML。我没有得到任何智能感知(我通常会得到建议),并且我得到了多个布局的强制转换异常。 XML 有问题吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    >
    <LinearLayout android:id="@+id/topSpacer"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".33"
        android:orientation="horizontal"
        ></LinearLayout>
    <LinearLayout
        android:id="@+id/bottomSection"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".67"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:id="@+id/loginSection"
            android:layout_width="320dip"
            android:layout_height="fill_parent"
            android:background="#FFFFFF"
            >
            <TextView
                android:text="" 
                android:id="@+id/errorMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
            <TextView 
                android:text="Please login using your active directory credentials." 
                android:id="@+id/loginMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
            <AutoCompleteTextView 
                android:id="@+id/loginUsername" 
                android:text="Username" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
            />
            <EditText
                android:id="@+id/loginPassword"
                android:text="Password"
                android:password="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            />
            <Button
                android:id="@+id/loginButton"
                android:text="Login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            />
            <TextView 
                android:text="results" 
                android:id="@+id/resultsMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

I need a box within a box, with ~33% of empty room at the top of the first box, and then another box below that. I need it to be centered. I would like this to essentially fill_parent on a mobile phone, but on a tablet (the primary device for this app), it should remain the same size but be centered in the middle of the screen. How can I do this?

I'm including to examples since it's a little hard for me to explain.

Mobile

|---------------------------|
|     ~33% Blank space      |
|                           |
|  |---------------------|  |
|  |                     |  |
|  |                     |  |
|  |      Login Box      |  |
|  |      Password       |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |                     |  |
|  |---------------------|  |
|---------------------------| 

Tablet

|---------------------------------------|
|           ~33% Blank space            |
|                                       |
|        |---------------------|        |
|        |                     |        |
|        |                     |        |
|        |      Login Box      |        |
|        |      Password       |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |                     |        |
|        |---------------------|        |
|---------------------------------------| 

Edit Here is the XML I currently have. I'm not getting intellisense for anything, (I usually get suggestions), and I'm getting a cast exception with multiple layouts. Is there something wrong with the XML?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    >
    <LinearLayout android:id="@+id/topSpacer"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".33"
        android:orientation="horizontal"
        ></LinearLayout>
    <LinearLayout
        android:id="@+id/bottomSection"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight=".67"
        android:orientation="horizontal"
        >
        <LinearLayout
            android:id="@+id/loginSection"
            android:layout_width="320dip"
            android:layout_height="fill_parent"
            android:background="#FFFFFF"
            >
            <TextView
                android:text="" 
                android:id="@+id/errorMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
            <TextView 
                android:text="Please login using your active directory credentials." 
                android:id="@+id/loginMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
            <AutoCompleteTextView 
                android:id="@+id/loginUsername" 
                android:text="Username" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
            />
            <EditText
                android:id="@+id/loginPassword"
                android:text="Password"
                android:password="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            />
            <Button
                android:id="@+id/loginButton"
                android:text="Login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
            />
            <TextView 
                android:text="results" 
                android:id="@+id/resultsMessage" 
                android:layout_height="wrap_content" 
                android:layout_width="fill_parent" 
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哀由 2024-12-19 07:24:25

尝试使用 weightSum 属性的线性布局组合。另请注意,正如您在平板电脑上提到的那样,内盒的宽度是固定的,您不希望它重新调整大小。

试试这个:

*编辑:我使用相对布局使内框居中*

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:weightSum="1">
<LinearLayout android:id="@+id/thirtyThreePercentSpacer" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_weight=".33">
</LinearLayout>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_weight=".67">

            <LinearLayout android:id="@+id/innerBox" 
                    android:layout_width="320dip" 
                    android:layout_height="fill_parent"
                    android:layout_centerInParent="true">
            </LinearLayout>

  </RelativeLayout>
</LinearLayout>

Try a combination of linear layouts that utilize the weightSum property. Also notice that the inner box's width is fixed as you mentioned on the tablet you don't want it to re-size.

Try this:

*EDIT: i used a relative layout to get the inner box centered *

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  android:weightSum="1">
<LinearLayout android:id="@+id/thirtyThreePercentSpacer" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal"
        android:layout_height="0dp"
        android:layout_weight=".33">
</LinearLayout>
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_weight=".67">

            <LinearLayout android:id="@+id/innerBox" 
                    android:layout_width="320dip" 
                    android:layout_height="fill_parent"
                    android:layout_centerInParent="true">
            </LinearLayout>

  </RelativeLayout>
</LinearLayout>
一袭白衣梦中忆 2024-12-19 07:24:25

您可以为智能手机和平板电脑使用不同的布局。

将其放入您的 layout 文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="login box"/>
    </LinearLayout>

</LinearLayout>

对于平板电脑,您可以在 layout-large 下使用类似的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="login box"/>
    </LinearLayout>

</LinearLayout>

在这种情况下,您应该根据需要调整登录框的宽度。

也许您可以使用相同的布局做一些事情,但一般来说,这些设备类的布局会非常不同。为了改进这一点,您可以设计一次登录框,然后从这些布局中包含它(请参阅http://developer.android.com/resources/articles/layout-tricks-reuse.html)。

You can use different layouts for smartphones and tablets.

Put that in your layout folder:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="login box"/>
    </LinearLayout>

</LinearLayout>

And for tablets you can use something like this under layout-large:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_gravity="center_horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="login box"/>
    </LinearLayout>

</LinearLayout>

In that case you should adjust the width of the login box to your needs.

Maybe you could do some things with the same layout but in general the layouts for those device classes will differ extremely. To improve this you can design the login box once and include it from those layouts (see http://developer.android.com/resources/articles/layout-tricks-reuse.html).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文