【发布时间】: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