【问题标题】:Android Relative Layout In TableRow followed by TableRow with Linear LayoutTableRow 中的 Android 相对布局,后跟具有线性布局的 TableRow
【发布时间】:2017-03-26 10:00:17
【问题描述】:

我目前正在开发一个在 Android 6 上运行的 Android 应用程序。我对 Android 开发完全陌生,但有一点 WPF 经验,所以我认为 XML 会类似。以下是我希望视图的样子:

这是我目前要解决这个问题的XML:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_guimain"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="2"
    tools:context="indoorpilot.indoorpilot.GUIMain">
    <TableRow>
        <!--android:layout_width="match_parent">-->
        <TextView
            android:id="@+id/textView"
            android:layout_weight="1"
            android:gravity="center_horizontal"
            android:text="Building Label"/>
    </TableRow>


    <TableRow>
        <RelativeLayout
            android:layout_weight="1">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@drawable/mclaurysecondfloor"
                android:id="@+id/imageView2"/>
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:srcCompat="@android:drawable/presence_online"
                android:="@+id/markerImage"/>

        </RelativeLayout>
    </TableRow>

    <TableRow>
        <LinearLayout
            android:layout_weight="1"
            style="?android:attr/buttonBarStyle">
            <Button
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/contextual_info"
                android:onClick="ShowContextualInformation"/>
            <Button
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/scan_button"
                android:onClick="SetRSSIDistanceValues"/>
        </LinearLayout>
    </TableRow>
</TableLayout>

这在大型设备(平板电脑)上运行良好,但在 Android Studio View Renderer 或任何类型的手机上都无法正常显示。按钮未显示,因为包含 RelativeLayout 的 TableRow 最终占据了整个屏幕...

【问题讨论】:

  • 使用RelativeLayout 代替TableLayout 作为父级。并将顶行与父级对齐,底行与父级底部对齐。 RelativeLayout 为您提供各种对齐方式。

标签: android android-layout android-relativelayout tablelayout tablerow


【解决方案1】:

我已经在 android studio 之外完成了这项工作,但可以帮助你,如果你需要我可以为你编辑。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_guimain"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="2"
    tools:context="indoorpilot.indoorpilot.GUIMain">

    <TextView
        android:id="@+id/textView"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Building Label"/>



        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView"
            android:layout_above="@+id/buttons_container"
            app:srcCompat="@drawable/mclaurysecondfloor"
            android:id="@+id/imageView2"/>

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView"
            app:srcCompat="@android:drawable/presence_online"
            android:id="@+id/markerImage"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_below="@+id/imageView2"
        android:layout_height="wrap_content"
        android:layout_orientation="horizontal"
        android:id="@+id/buttons_container"
        style="?android:attr/buttonBarStyle">
        <Button
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/contextual_info"
            android:onClick="ShowContextualInformation"/>
        <Button
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/scan_button"
            android:onClick="SetRSSIDistanceValues"/>
    </LinearLayout>

</RelativeLayout>

您不需要TableLayout。你需要的是一个RelativeLayout,它的顶部有一个TextView,下面有两个ImageViews,还有一个包含视图底部的两个按钮的LinearLayout。对于较小的屏幕尺寸,您可能需要包含此 RelativeLayoutScrollView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    相关资源
    最近更新 更多