【问题标题】:Android layout correct alignmentAndroid布局正确对齐
【发布时间】:2013-07-03 05:54:04
【问题描述】:

我正在做一个项目,但遇到了一些问题。我没有经常使用布局,所以我不知道这是否可能。

在图片中,我添加了项目模型。对于较低的屏幕分辨率,它看起来不错,但是当我将应用程序放在 5 英寸设备上时,这个橙色区域太高了,我想知道是否有可能以某种方式使该区域居中。

橙色区域是一个LinearLayout,所有的东西都包裹在LinearLayout里面。希望你明白我的意思我在问什么。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff0000"
    android:orientation="vertical"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <LinearLayout
        android:id="@+id/first"
        android:layout_width="match_parent"
        android:layout_height="76dp"
        android:background="#175797"
        android:orientation="vertical" >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Subject" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/second"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#00FFFF"
        android:orientation="vertical" >

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Send" />
    </LinearLayout>

    <LinearLayout
        android:layout_gravity="bottom"
        android:id="@+id/third"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#FFA500"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

这是模型的代码。对于项目来说几乎是一样的

【问题讨论】:

  • 发布您的布局 xml 文件代码。

标签: android android-layout android-xml


【解决方案1】:

试试这个!

我尝试将线性布局放在相对布局中并定义其重力。我检查了各种分辨率。试一试

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff0000"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp" >

<LinearLayout
    android:id="@+id/first"
    android:layout_width="match_parent"
    android:layout_height="76dp"
    android:background="#175797"
    android:orientation="vertical" >

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Subject" />
</LinearLayout>

<LinearLayout
    android:id="@+id/second"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#00FFFF"
    android:orientation="vertical" >

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Send" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    >

    <LinearLayout
        android:id="@+id/third"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="#FFA500"
        android:gravity="center"
        android:orientation="horizontal" >
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    您可以尝试@g00dy 回答,否则

    您必须为平板手机(即 5 英寸设备)创建单独的布局

    Here in this link 他们解释了我们如何放置我们的平板电脑布局...

    在 android 开发者网站上,他们已经解释了 android 如何管理不同屏幕尺寸的布局 check this link

    【讨论】:

      【解决方案3】:

      可以试试android:layout_gravity="center" 应该可以解决问题:) 另外,您可以添加android:layout_centerInParent="true",也可以查看this thread

      【讨论】:

        【解决方案4】:

        为了给所有android设备和平板电脑提供xml的兼容性,请使用Relative Layout作为父视图并根据您的要求设置组件。 请点击此链接以获取更多参考:http://developer.android.com/reference/android/widget/RelativeLayout.html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-03
          • 1970-01-01
          • 2011-09-16
          • 1970-01-01
          • 2014-08-08
          • 1970-01-01
          • 1970-01-01
          • 2011-05-17
          相关资源
          最近更新 更多