繁体   English   中英

同一行上的两个TextView

[英]Two TextViews on the same line

希望这是一个简单的

我想在同一行上的两个textview左对齐。

目前,我的布局如图所示:

在此处输入图片说明

我想要这种布局(注意我修改了油漆图):

在此处输入图片说明

这是我目前拥有的xml:

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:id="@+id/txtSetItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:layout_weight="2" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false"/>



<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="false"

    android:focusable="false" />

<TextView
    android:id="@+id/txtSetAmount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false" />



<TextView
    android:id="@+id/txtSetPrice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" 
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false"/>

将第一个TextView的权重更改为0(或小于1的值)。 您给它们相等的权重,而LinearLayout则以使其宽度相等的方式放置它们,这不是您想要的。

嗯,您的布局不应该编译-TextView对象上没有android:layout_width属性。

要解决此问题,请删除layout_weight并将layout_width设置为wrap_content

无论如何,在这里:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="This"
/>
<TextView  
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="this"
/>
</LinearLayout>

不要用weight layout_width上使用wrap_content

这将为您提供所需的东西:

<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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This"
        android:paddingLeft="10dp" />

</LinearLayout>

一种解决方法是使用相对布局。 您可以通过更改textView2上的android:layout_marginLeft属性来更改两个textview的“关闭”或“远”距离。 以下属性:android:layout_alignBaseline =“ @ + id / textView1”确保tv2与tv1对齐。

<?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="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_marginLeft="32dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="TextView" />

</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM