【问题标题】:How to include an image (would take 50% of the height) and several texts views on the same layout?如何在同一布局中包含图像(将占用 50% 的高度)和多个文本视图?
【发布时间】:2015-05-19 18:47:26
【问题描述】:

这几天我一直在处理一个我自己无法解决的问题。

我正在构建一个 Android 应用程序,在该应用程序上将显示新闻列表,按页面显示一条新闻或在 Android 手机上查看。我只需要向下滚动即可查看第二条、第三条等新闻。基本上我希望有一个图像可以占据屏幕的前半部分,而屏幕的另一半部分将显示文本视图。

我找到了几个有趣的答案,例如 Make image appear half of the screenAndroid: 2 relative layout divided in half screen nut 没有一个可以解决我的问题。

如您所见,我有 2 个布局,它们是线性布局的一部分。

我无法在一个占布局高度 100% 的视图中仅获取一个新闻(1 个图像和 4 个文本视图)的内容。我正在为每条新闻寻找相同的结果。

它应该类似于 Flipboard 应用,以防你听说过这个应用。

这是我的 xml 文件:

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/news_hockey2"
            android:adjustViewBounds="true"
            />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="47dp"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="title"
                android:id="@+id/title"
                android:textIsSelectable="true"
                android:layout_weight="0.1" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:id="@+id/date_of_pub_list"
                android:text="date_of_pub"
                android:layout_weight="0.1" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="121dp"
                android:text="article"
                android:id="@+id/article"
                android:layout_marginBottom="10dp"
                android:layout_weight="0.2"
                android:phoneNumber="false" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="35dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="link"
                android:id="@+id/link"
                android:layout_marginBottom="15dp"
                android:layout_weight="0.1" />

        </LinearLayout>
    </RelativeLayout>

</LinearLayout>

【问题讨论】:

    标签: android-layout


    【解决方案1】:

    你没有理由用RelativeLayouts 中的textViews 包装ImageView 或LinearLayout。删除这些并将 Linearlayout 和 ImageView 设置为:

    android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-01
      • 2019-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多