Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 两个视图之间的文本_Android_Android Layout - Fatal编程技术网

Android 两个视图之间的文本

Android 两个视图之间的文本,android,android-layout,Android,Android Layout,我希望取得以下成果: 电视:文本视图 IV:ImageView 所有四个视图都是从数据库填充的,因此具有动态宽度(IV除外) ID从左到右: icon tv_top tv_bottom tv_right 我在本教程()中做了这个相对布局: 有没有比编程方式(可能是xml方式)更好的方法?我应该使用线性布局吗?在你的文本视图中间,不要使用 Android:LayOutTracePalEngIt= =“true”< /C>附加余量,但将它们设置在右视图的左边和右边的TeXVIEW中,并将宽度

我希望取得以下成果:

  • 电视:文本视图
  • IV:ImageView

所有四个视图都是从数据库填充的,因此具有动态宽度(IV除外)

ID从左到右:

icon
tv_top
tv_bottom
tv_right
我在本教程()中做了这个相对布局:


有没有比编程方式(可能是xml方式)更好的方法?我应该使用线性布局吗?在你的文本视图中间,不要使用<代码> Android:LayOutTracePalEngIt= =“true”< /C>附加余量,但将它们设置在右视图的左边和右边的TeXVIEW中,并将宽度设置为<代码> Matkh Prime< /代码>。这将使它们直接位于其他两个视图之间

两个文本视图的示例:

 <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="14sp"
        >
</TextView>

<TextView
        android:id="@+id/tv_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:textSize="21.5sp"
        android:singleLine="true">
</TextView>


并且对两个视图再加上一个小的边距,这样它们就不会直接粘贴在其他视图上。

在您的文本视图中间,不要使用<代码> Android:LayOutTracePaleNeWiTe=“true”< /Cord>附加余量,但将它们设置为左ImageView的右侧和右TextView的左侧,并将宽度设置为
match\u parent
。这将使它们直接位于其他两个视图之间

两个文本视图的示例:

 <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="14sp"
        >
</TextView>

<TextView
        android:id="@+id/tv_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:textSize="21.5sp"
        android:singleLine="true">
</TextView>


并且对两个视图再加上一个小的边距,这样它们就不会直接粘贴在其他视图上。

在您的文本视图中间,不要使用<代码> Android:LayOutTracePaleNeWiTe=“true”< /Cord>附加余量,但将它们设置为左ImageView的右侧和右TextView的左侧,并将宽度设置为
match\u parent
。这将使它们直接位于其他两个视图之间

两个文本视图的示例:

 <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="14sp"
        >
</TextView>

<TextView
        android:id="@+id/tv_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:textSize="21.5sp"
        android:singleLine="true">
</TextView>


并且对两个视图再加上一个小的边距,这样它们就不会直接粘贴在其他视图上。

在您的文本视图中间,不要使用<代码> Android:LayOutTracePaleNeWiTe=“true”< /Cord>附加余量,但将它们设置为左ImageView的右侧和右TextView的左侧,并将宽度设置为
match\u parent
。这将使它们直接位于其他两个视图之间

两个文本视图的示例:

 <TextView
        android:id="@+id/tv_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="end"
        android:singleLine="true"
        android:textSize="14sp"
        >
</TextView>

<TextView
        android:id="@+id/tv_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_toRightOf="@id/icon"
        android:textSize="21.5sp"
        android:singleLine="true">
</TextView>


并再次为两个视图添加一个小边距,这样它们就不会直接粘在其他视图上。

您可以使用
LinearLayout
layout\u weight

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"/>

    <!-- weight 3 of 4 = 3/4 of available width for this LinearLayout -->
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@+id/tv_bottom"
            android:text="Top line"/>

        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom line"/>

    </RelativeLayout>

    <!-- weight 1 of 4 = 1/4 of available width for this TextView -->
    <TextView
        android:id="@+id/tv_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Right text"/>

</LinearLayout>

您可以使用
LinearLayout
layout\u weight
进行此操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"/>

    <!-- weight 3 of 4 = 3/4 of available width for this LinearLayout -->
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@+id/tv_bottom"
            android:text="Top line"/>

        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom line"/>

    </RelativeLayout>

    <!-- weight 1 of 4 = 1/4 of available width for this TextView -->
    <TextView
        android:id="@+id/tv_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Right text"/>

</LinearLayout>

您可以使用
LinearLayout
layout\u weight
进行此操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"/>

    <!-- weight 3 of 4 = 3/4 of available width for this LinearLayout -->
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@+id/tv_bottom"
            android:text="Top line"/>

        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom line"/>

    </RelativeLayout>

    <!-- weight 1 of 4 = 1/4 of available width for this TextView -->
    <TextView
        android:id="@+id/tv_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Right text"/>

</LinearLayout>

您可以使用
LinearLayout
layout\u weight
进行此操作

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="?android:attr/listPreferredItemHeight"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"/>

    <!-- weight 3 of 4 = 3/4 of available width for this LinearLayout -->
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@+id/tv_bottom"
            android:text="Top line"/>

        <TextView
            android:id="@+id/tv_bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Bottom line"/>

    </RelativeLayout>

    <!-- weight 1 of 4 = 1/4 of available width for this TextView -->
    <TextView
        android:id="@+id/tv_right"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Right text"/>

</LinearLayout>

这可以在不使用任何其他布局的情况下实现。我们所需要的只是一个相对的

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

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/iv_left"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:singleLine="true"
        android:text="ABC"
        android:textSize="24sp"
        android:background="#888"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_below="@id/et_top"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="14sp" />

</RelativeLayout>

您可以根据自己的意愿设置填充和边距

[编辑]哎呀,我没注意到正确的是文本视图。。。代码已更新


希望有帮助:)

这可以通过不使用任何其他布局来实现。我们所需要的只是一个相对的

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

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/iv_left"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:singleLine="true"
        android:text="ABC"
        android:textSize="24sp"
        android:background="#888"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_below="@id/et_top"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="14sp" />

</RelativeLayout>

您可以根据自己的意愿设置填充和边距

[编辑]哎呀,我没注意到正确的是文本视图。。。代码已更新


希望有帮助:)

这可以通过不使用任何其他布局来实现。我们所需要的只是一个相对的

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

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/iv_left"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:singleLine="true"
        android:text="ABC"
        android:textSize="24sp"
        android:background="#888"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_below="@id/et_top"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="14sp" />

</RelativeLayout>

您可以根据自己的意愿设置填充和边距

[编辑]哎呀,我没注意到正确的是文本视图。。。代码已更新


希望有帮助:)

这可以通过不使用任何其他布局来实现。我们所需要的只是一个相对的

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

    <ImageView
        android:id="@+id/iv_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/iv_left"
        android:layout_alignParentRight="true"
        android:gravity="center"
        android:singleLine="true"
        android:text="ABC"
        android:textSize="24sp"
        android:background="#888"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/et_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/iv_left"
        android:layout_toLeftOf="@id/tv_right"
        android:layout_below="@id/et_top"
        android:singleLine="true"
        android:text="@string/app_name"
        android:textSize="14sp" />

</RelativeLayout>

您可以根据自己的意愿设置填充和边距

[编辑]哎呀,我没注意到正确的是文本视图。。。代码已更新



希望有帮助:)

你是说线性布局中的电视顶部和电视底部?我已经编辑了我的问题,以便清除哪个ID属于wich图像。@AndreasLympouras,我删除了你的大部分细节,以便更容易理解。我更新了我的答案,因为我把ID弄混了。我还在两个中间的
文本视图
中使用了
RelativeLayout
,展示了一种可能的方式,以我认为您想要的方式显示这些内容。我将把您想要的维度放回一个练习中给您——我特意为未来的读者提供了更通用的答案。谢谢您的回答,但我相信
layout\u-toLeftOf
layout\u-toRightOf
是更简单的解决方案。@AndreasLympouras当然,您的电话。这实际上取决于您想要如何定义大小缩放。这将在所有屏幕大小上均匀/成比例地缩放
中间文本视图
右侧文本视图
的宽度,而root
RelativeLayout
选项将根据
中间文本视图的内容调整大小
,并将
右侧文本视图
保留在其
换行内容
(或特定定义)处宽度。无论哪一个适合你的用例,我都会把它留在这里,作为未来有类似问题的人的一个选择。你是说线性布局中的tv_top和tv_bottom?我已经编辑了我的问题,以便清除哪个ID属于wich图像。@AndreasLympouras,我删除了你的大部分细节,以便更容易理解。我更新了我的答案,因为我把ID弄混了。我还在中间的两个
文本视图中使用了
RelativeLayout
,以显示在