How to display 2 text images in one line in Android

I have an xml layout file:

<?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:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/HostName" android:layout_weight="0" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0" /> </LinearLayout> 

when I do the above, my output is as follows:

enter image description here

But my requirement is to get my result below:

 | text1: text2 | 

Can anyone help?

+6
source share
8 answers

Use the following code

enter image description here

Updated :

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:weightSum="1" > <TextView android:id="@+id/titlename" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:gravity="center" android:text="hi" /> <TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:gravity="center" android:text="2hi2" /> </LinearLayout> 
+15
source

I would take the weight attribute. This is the only thing I can see that it would be to cheat. Also note that fill_parent deprecated and match_parent should be used match_parent

Or, if you need weight for any reason, change each to 1 if you want each one to occupy half the screen.

Also, if you use weight , then you must set android:layout_width="0dp"

0
source

Set android:orientation="horizontal" for your LinearLayout.

0
source

Update your layout as shown below, you do not want to include the weight = 0 attribute unless you set weightSum on the parent LinearLayout. The pros of weight and weightSum are not suitable for what you are looking for.

 <?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:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/HostName" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
0
source

You can try using RelativeLayout

With this, you can set alignParentTop="true" in the titlename element and set the name alignTo="@id/titlename" and toRightOf="@id/titlename" in the name element

0
source

Have android: layout_width as 0dp. Then set 0.5 weights for each text view. Each of the text fields will fill half the width of the parent layout. Sort of:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/titlename" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/HostName" android:layout_weight="0.5" /> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" /> </LinearLayout> 
0
source

Actually your original code works fine in my ADT. Have you changed the size of the text or the filling of the line in Java code that updates the second text?

0
source
 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/HostName" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 
0
source

Source: https://habr.com/ru/post/943468/


All Articles