Question about laying widget on a RelativeLayout

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Question about laying widget on a RelativeLayout

Postby pofak » Tue Dec 15, 2009 2:58 am

I need to place three TextView in one row in a RelativeLayout. The width of the two at either end should be wrap_content. The one at the center needs to fill the space left between these two.

I cannot figure out how to do this. Any idea? I am using XML in the reslayout folder.

TIA
pofak
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sat Dec 12, 2009 1:07 am

Top

Postby THRESHE » Tue Dec 29, 2009 4:25 pm

Stick first text view to the top (android:layout_alignParentTop="true"). And place second below the first. Then place third below the second and set its android:layout_alignParentBottom="true"

That should do it
THRESHE
Freshman
Freshman
 
Posts: 5
Joined: Sun Dec 13, 2009 11:38 am

Postby achie1266 » Tue Dec 29, 2009 7:15 pm

the solution above will work when you set your relativelayout to be a scrollable container. Or else if of of the views is filling up the entire screen you will not be able to see the other views. The layout that THRESHE showed is still correct though.
That also explains the case where you have linear layout where textviews are arranged vertically.
Have a look at this example. It has two relativelayouts one will explain the horizontal layout and the other vertical.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.     android:layout_width="fill_parent"
  6.  
  7.    android:layout_height="fill_parent">
  8.  
  9.     <RelativeLayout
  10.  
  11.         android:layout_width="fill_parent"
  12.  
  13.         android:layout_height="wrap_content"
  14.  
  15.         android:id="@+id/top_rel_layout"
  16.  
  17.        android:layout_alignParentTop="true">
  18.  
  19.         <TextView
  20.  
  21.             android:text="text 1"
  22.  
  23.             android:id="@+id/top_rel_lay_text1"
  24.  
  25.             android:layout_width="wrap_content"
  26.  
  27.             android:layout_height="wrap_content"
  28.  
  29.             android:layout_alignParentLeft="true"
  30.  
  31.             android:textStyle="bold"/>          
  32.  
  33.         <TextView
  34.  
  35.             android:text="text 2 XXXXXXXXXXXXXXXXXXXXGGGGGGGGGGGGGGGHHHHHHHHHHHHHHHHKKKKKKKKKKKKKKKKKKK"
  36.  
  37.             android:id="@+id/top_rel_lay_text2"
  38.  
  39.             android:layout_width="wrap_content"
  40.  
  41.             android:layout_height="wrap_content"
  42.  
  43.             android:layout_toRightOf="@+id/top_rel_lay_text1"
  44.  
  45.             android:layout_toLeftOf="@+id/top_rel_lay_text3" />        
  46.  
  47.         <TextView
  48.  
  49.             android:text="text 3"
  50.  
  51.             android:id="@+id/top_rel_lay_text3"
  52.  
  53.             android:layout_width="wrap_content"
  54.  
  55.             android:layout_height="wrap_content"
  56.  
  57.             android:layout_alignParentRight="true"/>
  58.  
  59.         </RelativeLayout>
  60.  
  61.           <TextView
  62.  
  63.               android:text="text 1 SSSSSSSSSSSSSSSSSSSSSSSSS"
  64.  
  65.               android:id="@+id/rel_lay_text1"
  66.  
  67.               android:layout_width="fill_parent"
  68.  
  69.               android:layout_height="wrap_content"
  70.  
  71.               android:layout_below="@+id/top_rel_layout"
  72.  
  73.               android:textStyle="bold"/>          
  74.  
  75.           <TextView
  76.  
  77.               android:text="text 2\ntex\ntext 2\ntex\ntext 2\ntex\ntext 2\ntex\ntext 2\ntex\ntext 2\ntex\ntext
  78.               2\ntex\ntext 2\ntex\ntext 2\ntex\n"
  79.  
  80.               android:id="@+id/rel_lay_text2"
  81.  
  82.               android:layout_width="fill_parent"
  83.  
  84.               android:layout_height="wrap_content"
  85.  
  86.               android:layout_below="@+id/rel_lay_text1"
  87.  
  88.               android:layout_above="@+id/rel_lay_text3" />        
  89.  
  90.           <TextView
  91.  
  92.               android:text="text 3 OOOOOOOOOOOOOOOOOOOOO"
  93.  
  94.               android:id="@+id/rel_lay_text3"
  95.  
  96.               android:layout_width="fill_parent"
  97.  
  98.               android:layout_height="wrap_content"
  99.  
  100.               android:layout_alignParentBottom="true" />
  101.  
  102. </RelativeLayout>
  103.  
  104.  
Parsed in 0.007 seconds, using GeSHi 1.0.8.4
If you run this you will observe that you will have to customize it to have scrollbars or keep the textviews in scroll views to let the user scroll the text if needed.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests