【问题标题】:How to make application responsive for all android devices?如何使应用程序对所有 android 设备都有响应?
【发布时间】:2014-12-11 05:08:14
【问题描述】:

我正在创建一个应用程序,我希望我的应用程序打开时视图没有任何问题,我尝试为此使用 weightsum 属性,但我不知道为什么它在不同的设备上一直显示不同的视图,检查一下..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:background="@drawable/backgroundgd"
 android:weightSum="100.0"
 >
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/another"
    />
    <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" 
    android:background="@drawable/secondpart"
    />
 </LinearLayout>

在 bluestack 中它看起来像这样

【问题讨论】:

  • 请放上视图的截图,它们的显示方式有何不同。
  • 你能分享想要的输出吗?
  • @SweetWisherツ 我希望我的视图在所有设备上看起来都一样
  • 您的要求是什么?
  • 是的,但要更具体地说明您想在 xml 中添加的所有控件,请关注 this

标签: android android-linearlayout android-layout-weight


【解决方案1】:

嗨,约翰逊,这个例子可能会对你有所帮助。

 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="5">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:text="2" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="3" />

【讨论】:

    【解决方案2】:

    android:layout_widthandroid:layout_height 更改为"match_parent"LinearLayout

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical"
     android:background="@drawable/backgroundgd"
     android:weightSum="100.0">
        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_cntent"
        android:text="@string/hello_world" 
        android:background="@drawable/another"
        />
        <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:background="@drawable/secondpart"
        />
     </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      在容器LinearLayout中给出match_parent的宽度和高度:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:background="@drawable/backgroundgd"
      >
          <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" 
          android:background="@drawable/another"
          />
          <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello_world" 
          android:background="@drawable/secondpart"
          />
      </LinearLayout>
      

      另外,您使用的重量不正确。

      权重和权重用于在某些元素之间平均划分屏幕部分。所以如果你在容器布局中给出 weightsum = "3",并且它的 3 个子布局提到 weight="1",那么所有三个子布局都使用了 33.33% 的容器布局部分。并且此时宽度和高度根据容器布局的方向保持为0 dp。

      【讨论】:

      • 重量和重量的用途是什么
      • 权重和权重用于在某些元素之间平均划分屏幕部分。所以如果你在容器布局中给出 weightsum = "3",并且它的 3 个子布局提到 weight="1",那么所有三个子布局都使用了 33.33% 的容器布局部分。并且此时宽度和高度根据容器布局的方向保持为0 dp。
      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多