【问题标题】:Android-Make TextView to fill all the remaining space in layoutAndroid-Make TextView 填充布局中的所有剩余空间
【发布时间】:2017-01-03 21:32:32
【问题描述】:

我有一个带有 ImageViewTextView 的新闻活动,其中包含新闻内容。我想让新闻内容填满所有空间。

新闻内容填满了图像视图右侧的所有空间,因此文本被放置在图片右侧,但图片下方是空闲空间。我决定将ImageViewTextView 放在分离的线性布局中。但是在图像右侧的区域被填充后,文本消失了。

问题显示here

我的问题是:

  1. 如何将剩余的文本放在图像视图下?

  2. 如何让图片保持他的高宽比?

这是 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutorialsbuzz.phrasebook.NewsActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textViewHeaderNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewDateNews"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="header"
            android:textColor="@color/mycolor"
            android:textSize="16dp"
            android:textStyle="bold|italic" />

        <LinearLayout
            android:id="@+id/newslayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textViewHeaderNews"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageViewNews"
                android:layout_width="165dp"
                android:layout_height="165dp"
                android:layout_margin="2dp"
                android:layout_weight="0"
                android:scaleType="fitXY"
                android:src="@drawable/world"/>

            <TextView
                android:id="@+id/textViewContentNews"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_marginTop="1dp"
                android:layout_weight="1"
                android:text="jklasdf as ;AJFWADLHF LAS HFLAF VANLASN: l galgsalgngan  ;wafasldwlaf alla lnaf alf lg;aeguewaf hwla hfnalgh wa
                j;fklasgj;s;nva;   alsf las ps z; laszfj alsz vzlxg a lrg z;dnlxfdj glzf ;f glaerzlls az
                ;asdjk  aF{ af[ [D{j  as ;as ; wjv a jkn da weq  g ; aeskj; g ;eakr;ja r"
                android:textSize="14dp" />
        </LinearLayout>

        <TextView
            android:id="@+id/textViewDateNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="2dp"
            android:layout_marginTop="2dp"
            android:text="date"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/textViewViewsNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/newslayout"
            android:layout_margin="3dp"
            android:text="views"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/textViewRating"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/newslayout"
            android:layout_margin="3dp"
            android:text="rating"
            android:textSize="13dp" />

        <TextView
            android:id="@+id/textViewRatingNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/spinnerRatingNews"
            android:layout_margin="4dp"
            android:layout_toLeftOf="@+id/spinnerRatingNews"
            android:text="Գնահատեք."
            android:textSize="14dp" />

        <Spinner
            android:id="@+id/spinnerRatingNews"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/textViewRating"
            android:layout_margin="3dp" />

    </RelativeLayout>
</ScrollView>

【问题讨论】:

    标签: android xml android-layout textview


    【解决方案1】:

    嗯,解释起来很复杂(无论如何对我来说)。 没有办法用 xml 来做到这一点,但你可以通过代码来做到这一点,我会参考你的帖子,我曾经用你想要的方式来实现相同的结果。 (将 textview 包裹在其他视图周围)。

    Read this post

    上面是我用过的,效果很好。

    您在评论中要求调整图像 btu 的大小以保持其比例。

    This is a post关于调整图片大小的话题。

    首先检查图像是否比 X 大,然后使用符合您需要的因子调整大小。

    【讨论】:

    • 谢谢。以及如何让ImageView保持他的高宽比?
    • 好吧,你硬编码了它的高度/宽度,所以你不能保持他原来的高度/宽度比。您可以改用 wrap_content。
    • 但是对于不同的新闻,我需要不同的图像,具有不同的高度和宽度。所以我需要将它们放在我的 ImageView 中,保持它们的视图。
    • 嗯,我没从你的评论中明白“wrap_content”有什么问题?
    • 因为可能会上传大尺寸的图片(例如内容作者)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多