簡體   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