【问题标题】:LinearLayout Beneath LinearLayout full width?LinearLayout 在 LinearLayout 全宽之下?
【发布时间】:2015-05-06 18:07:57
【问题描述】:

我有一个 ImageView 和一个 TextView,它们是我的 Android UI 中的唯一项目。目前 TextView 与 ImageView 处于相同的 LinearLayout 中,因此文本有时会运行到 2 行。我想在自己的行上获取文本,并具有父元素的全宽,因此它将是屏幕的整个宽度。到目前为止,我尝试的所有操作都将 ImageView 移到了窗口外或使其非常小。

<LinearLayout 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"
    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="4"
    android:gravity="center"
    tools:context=".MainActivity"
    android:baselineAligned="false">

    <LinearLayout
        android:layout_width="0dp"
        android:orientation="vertical"
        android:layout_weight="2"
        android:gravity="center_vertical"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/activity_main_imageview_gototags"
            android:src="@drawable/img_main_gototags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:onClick="imageViewGoToTagsOnClickOnClick"
            android:adjustViewBounds="true"/>

        <TextView
            android:id="@+id/activity_main_textview_tagline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/activity_main_textview_tagline"
            android:textColor="@color/dark_blue"
            android:gravity="center"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 您希望文本显示在图像下方还是重叠显示在图像上方?
  • 图片下方@dora

标签: android xml android-layout android-linearlayout


【解决方案1】:

这就是你想要的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:gravity="center"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >


        <ImageView
            android:id="@+id/activity_main_imageview_gototags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:onClick="imageViewGoToTagsOnClickOnClick"
            android:scaleType="centerCrop"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5" />
    </LinearLayout>

<TextView
    android:id="@+id/activity_main_textview_tagline"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:singleLine="true"
    android:gravity="center"
    android:text="lfhjdhkjfhkjdhkjfhkdhkjfhdkshfkhskahdfkjdhkhkjfhdk"
    android:textColor="@android:color/holo_blue_bright"
    android:textSize="30sp" />

</LinearLayout>

我希望它有帮助。如果没有,请告诉我。

【讨论】:

  • 这实际上是超级接近。我怎样才能使它在垂直和水平的中心对齐?
  • 差不多了。我看不到您所做的更改,但我还需要使图像在中心水平对齐。
  • 再次编辑。如果有帮助,请支持答案。在之前的编辑中,我将父布局的重力置于中心位置,并使父布局高度与父布局高度匹配。
  • 是的!这正是它。谢谢一堆。我还不能投票,但我会尽快回来给你一个。我已将其标记为正确。
【解决方案2】:

不要给你的ImageView 的父级(LinearLayout0dp 的宽度改为match_parent,对于你的TextView 也有一个属性在文本视图android:maxLines 上对应于最多可以到达多少行,将其设置为 1,如果需要截断,也可以在文本视图上使用 android:ellipsize

【讨论】:

  • 使 layout_width="match_parent" 使整个 imageview 成为窗口的大小。我希望它保持 50% 的比例,但 TextView 到 math_parent
  • 好吧,我刚刚进行了实验,然后将LinearLayout 的宽度保持为 0dp,并将TextView 的宽度更改为match_parent 以及maxLines=1 应该可以正常工作
  • 就是这样,如果文本溢出父元素(即屏幕的 50%),则该行将被截断。我正在寻找使 TextView 位于图像下方,全宽(在本例中为父级的父级)。
  • 啊,这很棘手ImageView 的大小与您期望线性布局如何使用权重属性进行调整一样,并将TextView 放在其下方。一些实验应该会让你得到你想要的结果
  • 是的,我可以务实地完成一个相当不错的结果,我只是希望找到一种在 XML 中完全静态的方法。不过感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 2018-11-15
  • 2023-03-18
相关资源
最近更新 更多