android gridlayout 列宽超过父(gridlayout)宽度

标签 android android-layout android-view textview android-gridlayout

我正在使用 gridlayout 对我的 View 进行分组。在里面,我有两列。一列有一个子项 relativelayout,另一列有一个子项 linearlayout

我的linearlayout 方向是垂直。它有两个 View ,textview 和 imageview。

问题是当我在 textview 中键入文本时,文本很长,我看不到

  • textview 中的一些字符>
  • imageview 位于 textview
  • 下方

textview 宽度正在扩展并且并在进入第 2 行之前隐藏它的一些字符和 imageview(textview 是多行的)

我不想使用边距来解决这个问题,因为如果我使用它,并且 textview 中的文本很短,不需要的空间会出现在右边 TextView 的一侧。

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:columnCount="2"
        app:rowCount="1" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_gravity="fill" >
        </RelativeLayout>

       <LinearLayout
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_gravity="fill">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="12345...50" >
        </TextView>

        <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/vladmir putin"/>

     </LinearLayout>

     </android.support.v7.widget.GridLayout>

如何解决textview宽度超过gridlayout宽度的问题? 如果我输入 1234 一切正常。但是如果我输入从 150,我看不到一些数字,比如从 3032 和图片 View ?

最佳答案

为了解决这个问题,我使用了RelativeLayout而不是 LinearLayout并对齐我的 textviewimageview 的开头

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="1"
>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_gravity="fill" >
</RelativeLayout>

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_gravity="fill">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/profile"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="12345...50"
        android:layout_toStartOf="@id/profile">
    </TextView>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:src="@drawable/profile"
        android:id="@+id/profile"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true" />

</RelativeLayout>

关于android gridlayout 列宽超过父(gridlayout)宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37808475/

相关文章:

java - EditText、OnTouchListener 和 setSelection 在第一次触摸时不起作用

Android 设置文本对齐 toast 的中间

android - 如何禁用 Android View 中的任何事件?

android - 如何在屏幕关闭或打开另一个应用程序时保持应用程序运行?

java - Android 屏幕操纵杆问题

android - 升级应用程序后错误膨胀类 android.widget.ImageButton

android - 以编程方式将 View 添加到 ConstraintLayout

android - 使用 fragment 改变方向 - 错误的 Activity 开始

java - 如何在Android 7.0版本的通话界面上方显示弹窗

android - 如何在 RectShape 或 OvalShape 中显示 Android Canvas ShapeDrawable 中的文本?