【问题标题】:UI is not coming properly in actual deviceUI 在实际设备中无法正常显示
【发布时间】:2018-07-07 04:40:38
【问题描述】:

在我的项目中,我使用了两个选项卡,每个选项卡都有一个片段。它在模拟器上看起来不错,但在实际的 android 设备上却无法正常运行。无论设备的分辨率或尺寸如何,我都希望所有内容都能正确地适应屏幕。这只是一个标签的片段:

fragment_encode.xml

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/load_image_layout_margin_top"
        android:background="@drawable/layout_border">

        <ImageView
            android:id="@+id/loadImage"
            android:layout_width="match_parent"
            android:layout_height="@dimen/load_image_encode"
            android:gravity="center"
            android:paddingTop="2sp"
            android:contentDescription="@string/description_upload_image"
            android:scaleType="centerInside" />

        <TextView
            android:id="@+id/imageTextMessage"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="bottom|center"
            android:drawableStart="@drawable/ic_open_image"
            android:gravity="center"
            android:text="@string/description_upload_image"
            android:textAllCaps="true"
            android:textColor="@color/colorPrimary"
            android:textSize="20sp" />
    </FrameLayout>

    <EditText android:textColor="@color/colorPrimaryDark"
        android:gravity="left"
        android:id="@+id/passwordEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12sp"
        android:layout_marginRight="12sp"
        android:layout_marginTop="16sp"
        android:hint="@string/password_edit_text_hint"
        android:maxLength="16"
        android:inputType="textPassword" />

    <LinearLayout android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="0dp"
        android:layout_weight="1.0">

        <Button
            android:id="@+id/textToEncodeButton"
            android:layout_width="match_parent"
            android:layout_height="56sp"
            android:layout_margin="12sp"
            android:background="@color/colorButtonBackground"
            android:text="@string/enter_text"
            android:textColor="@color/colorButtonText"
            android:textSize="@dimen/button_text_size" />

        <Button
            android:id="@+id/encodeButton"
            android:layout_width="match_parent"
            android:layout_height="56sp"
            android:layout_marginLeft="12sp"
            android:layout_marginRight="12sp"
            android:background="@color/colorButtonBackground"
            android:onClick="decodeButtonClicked"
            android:text="@string/encode"
            android:textColor="@color/colorButtonText"
            android:textSize="@dimen/button_text_size" />

    </LinearLayout>

</LinearLayout>

这是设备的快照,其中编码按钮正在向下且不可见。

【问题讨论】:

  • 您是否尝试将按钮高度设置为wrap_content
  • 是的,我试过了,但按钮大小没有太大变化。我可以让它以某种方式响应吗?好的,我会尝试wrap_content
  • 由于您使用的是线性布局,我建议使用layout_weight 属性(按钮的,每个设置1)而不是专门设置视图的layout_height
  • @Moon layout_weight 不起作用,但 wrap_content 运行良好。我仍然在想,如果我在另一部不同尺寸或分辨率的手机上运行我的应用程序,它可能会被搞砸。

标签: android android-layout android-fragments android-button


【解决方案1】:

当视图可能超出屏幕尺寸时,不建议专门设置按钮的高度。您可以将按钮的android_height 设置为wrap_content

【讨论】:

    【解决方案2】:

    如果您使用带权重的线性布局,那么建议使用

    layout_heigth="0dp"
    layout_weight="1"
    

    只为父视图设置高度,不为子视图设置高度。 试试下面的代码,可能会有帮助。 如果当前的“1”不起作用(使用小于 1 的值),请调整 editText 的权重。

        <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/load_image_layout_margin_top"
            android:background="@drawable/layout_border">
    
            <ImageView
                android:id="@+id/loadImage"
                android:layout_width="match_parent"
                android:layout_height="@dimen/load_image_encode"
                android:gravity="center"
                android:paddingTop="2sp"
                android:contentDescription="@string/description_upload_image"
                android:scaleType="centerInside" />
    
            <TextView
                android:id="@+id/imageTextMessage"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="bottom|center"
                android:drawableStart="@drawable/ic_open_image"
                android:gravity="center"
                android:text="@string/description_upload_image"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="20sp" />
        </FrameLayout>
    </LinearLayout>
    <EditText android:textColor="@color/colorPrimaryDark"
    android:gravity="left"
    android:id="@+id/passwordEditText"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_marginLeft="12sp"
    android:layout_marginRight="12sp"
    android:layout_marginTop="16sp"
    android:hint="@string/password_edit_text_hint"
    android:maxLength="16"
    android:inputType="textPassword" />
    
    <LinearLayout android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="0dp"
    android:layout_weight="1">
    
    <Button
        android:id="@+id/textToEncodeButton"
        android:layout_width="match_parent"
        android:layout_height="56sp"
        android:layout_margin="12sp"
        android:background="@color/colorButtonBackground"
        android:text="@string/enter_text"
        android:textColor="@color/colorButtonText"
        android:textSize="@dimen/button_text_size" />
    
    <Button
        android:id="@+id/encodeButton"
        android:layout_width="match_parent"
        android:layout_height="56sp"
        android:layout_marginLeft="12sp"
        android:layout_marginRight="12sp"
        android:background="@color/colorButtonBackground"
        android:onClick="decodeButtonClicked"
        android:text="@string/encode"
        android:textColor="@color/colorButtonText"
        android:textSize="@dimen/button_text_size" />
    
    </LinearLayout>
    
        </LinearLayout>
    

    如果需要,更改线性布局的权重并将权重总和保持为 1 或 100。线性布局将根据所有屏幕自行调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-27
      • 2013-01-23
      • 2013-08-17
      • 2016-02-14
      相关资源
      最近更新 更多