Android布局均匀间距

标签 android android-layout layout textview imageview

这是我的编辑器和设备所显示内容的比较:

enter image description here

问题是:

  • 包含 TextView 的行应占屏幕的 50%
  • 包含图标的行应占据剩余的 50% 屏幕

它在编辑器中工作正常,但如您所见,布局在实际设备上的行为有很大不同。

我的问题在哪里以及如何解决?

用于图标的库是: https://github.com/code-mc/material-icon-lib

它直接从 ImageView 扩展。

布局 XML:

<TableLayout
    android:id="@+id/menuLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*" >

    <TableRow
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:layout_weight="1"
            android:orientation="horizontal"
            android:layout_height="match_parent"
            android:layout_width="match_parent">
            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/teenSelection"
                android:textColor="@color/WHITE"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="30sp"
                android:text="TEEN" />
            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/adultSelection"
                android:textColor="@color/WHITE"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="30sp"
                android:text="ADULT"/>
            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/seniorSelection"
                android:textColor="@color/WHITE"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:layout_weight="1"
                android:textStyle="bold"
                android:textSize="30sp"
                android:text="SENIOR"/>
        </LinearLayout>
    </TableRow>

    <TableRow
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <net.steamcrafted.materialiconlib.MaterialIconView
            android:layout_weight="1"
            android:id="@+id/confirmButton"
            app:materialIcon="check"
            app:materialIconColor="#fff"
            android:scaleType="center"
            android:layout_height="match_parent"
            android:layout_width="match_parent"/>
    </TableRow>

</TableLayout>

最佳答案

您已将两个TableRowsheight 设置为wrap_content,但它们必须像这样match_parent :

<TableRow
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

否则,高度重量 将被忽略。

关于Android布局均匀间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36572415/

相关文章:

java - 在停靠模式下启动我的应用程序

android - 当我将一个变量从一个 Activity 传递到另一个 Activity 时出现 NullPointer 异常

android - 我无法使用 Intent 进入下一个 Activity

java - 在 Android 中创建自定义 ListView 时出现 UnsupportedOperationException

android - 我最早可以在 View 子类中使用 findViewById 的时间是什么时候?

android - 为什么处理程序泄漏问题不会在主线程以外的线程中发生

android - 无法让 Joda-time 在 Android 上运行。

android - 高度大于屏幕高度的自定义 View 设计

android - 如何访问 BroadcastReceiver 中的 TextView 元素

c# - 简单高级的布局