简体   繁体   中英

Two TextViews on the same line

Hoping this is a simple one

I want two textviews on the same line aligned to the left.

Currently my layout is as shown:

在此处输入图片说明

I want this layout(note i modified the diagram on paint):

在此处输入图片说明

here is the xml i currently have:

   <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"/>

Change your first TextView's weight to 0 (or to something smaller than 1). You're giving them equal weight and LinearLayout places them in a way that gives them equal width, which isn't what you want.

hmm your layout shouldn't compile-- there is no attribute android:layout_width on the TextView objects.

To fix this, remove layout_weight and set layout_width to wrap_content

Anyways, here:

<?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>

Don't use weight . Use wrap_content on the layout_width .

This will get you what you are looking for:

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

one way to go about this would be to use relative layout . You can change how "close" or "far" apart the two textviews will be using changing the android:layout_marginLeft property on textView2. The following property: android:layout_alignBaseline="@+id/textView1" makes sure that tv2 is aligned with 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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