【问题标题】:Android spacing安卓间距
【发布时间】:2018-04-12 14:22:50
【问题描述】:

我在线性布局中对视图进行间距/布局时遇到问题。在模拟器中,布局看起来很完美。但是,当我在实际的 android 平板电脑上进行测试时,布局不正确。有关我在平板电脑上看到的图片,请参阅链接。

Device Layout Example

这就是我在模拟器上看到的:

Emulator Layout Example

我已经在 XML 中定义了视图:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
    android:id="@+id/sample"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="sample"
    android:textSize="14sp"/>

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

<EditText
    android:id="@+id/sample"
    android:layout_width="201dp"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/sample"
    android:hint=""
    android:inputType="numberDecimal"
    android:text="@={config.sample}"
    android:textAlignment="center"/>
</LinearLayout>

我真的不知道是什么原因导致这段代码在模拟器中看起来很好,但在设备上却被抬高了。我想我可以为 Text 视图使用定义的宽度,但这感觉不是正确的答案。基本上我需要一个文本视图、一个空格或边距,然后编辑文本视图全部均匀分布,即使文本视图中的文本可能具有不同的长度。

【问题讨论】:

    标签: android xml android-linearlayout


    【解决方案1】:

    您可以进行一些更改,例如

     <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="10">
    
        <TextView
            android:id="@+id/sample"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:text="sample"
            android:textSize="14sp"/>
    
        <Space
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="2"
            />
    
        <EditText
            android:id="@+id/sample"
            android:layout_width="0dp"
            android:layout_weight="4"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/sample"
            android:hint=""
            android:inputType="numberDecimal"
            android:text="@={config.sample}"
            android:textAlignment="center"/>
        </LinearLayout>
    

    【讨论】:

    • 所以这并没有完全解决我的问题,但它确实让我玩弄了 layout_weights 和 Space 视图,我最终能够使用它们来让模拟器和平板电脑上的一切看起来都正确.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多