android


How to separate layouts with weightSum in android?


I seem to be having issues understanding weightSum and LayoutWidth. My code is below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3">
<LinearLayout
android:background="#cccccc"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:padding="20sp"
android:textSize="30sp"
android:gravity="center_horizontal"
android:text="Cheapest Fare Option"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:background="#666666"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
My understanding of this is the 1st layout will take up 1/3 of the space and 2nd layout will take 2/3 of the space, but the reverse is happening, i.e. 1st layout is taking 2/3 and 2nd layout is taking 1/3
Why is this happening? Trying hard to understand this.
Taking your android:orientation="vertical", you want to get this ratio vertically therefore your android:layout_height attribute should be 0dp for both the child LinearLayout.
Set -
android:layout_height="0dp"
for both the inner LinearLayout.
Alternatively if you want to obtain the ratio horizontally then use android:orientation="horizontal" set -
android:layout_width="0dp"
for both the inner LinearLayout.
The thing is when you want to achieve ratio then you don't set that particular dimension (width or height) to match parent. Rather set it to 0dp so that Android can handle it for you automatically.
<LinearLayout
android:background="#cccccc"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:padding="20sp"
android:textSize="30sp"
android:gravity="center_horizontal"
android:text="Cheapest Fare Option"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:background="#666666"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
</LinearLayout>

Related Links

android.util.Log vs java.util.Logging - For writing log to a File
Roboto Font from Android 4.0 (Ice Cream Sandwich) [closed]
Communication with android Service
How to Download a File only if it has been uploaded on server
Android ListView strikethrough text
How to Get profile large image from Android Facebook Graph-API
Remove ListView separator(in the xml layout file) [duplicate]
How to implement Drag and drop in ListView to another Listview?
Starting activity from another application using only package name
Sound Pool Permissions
Drawing multiple views on android
Quickly switching and animating views in a FrameLayout messes up the views
How do I turn off or remove Android Visual Layout Editor?
tell if two android bluetooth devices are no longer actually connected to bluetooth socket
how to pass the same object one activity to another two different activities
simple example how to insert audio file in android

Categories

HOME
developer-tools
jelastic
saml-2.0
owl-carousel
r-googlesheets
database-normalization
settings
iis-6
crystal-reports-2013
p2p
fhir
enthought
angular-dart
capistrano3
fileserver
vuejs
template-engine
google-data-studio
symfony-console
topology
apache2.4
selinux
moses
opentk
mat
progress-bar
tizen-tv
hibernate-validator
rselenium
papaparse
hibernate-search
installscript
jwplayer6
latitude-longitude
apic
parse-tree
xcode7.3
c9.io
dcmtk
print-css
logback-groovy
opencmis
opennms
proget
configurationmanager
java-bytecode-asm
oracleclient
jqchart
oozie-coordinator
reshape
seq
mathcad
renaming
restore
vcloud-director-rest-api
microstation
xcode-server
segment-io
windows-azure-queues
beamer
ng-grid
jtwig
http-status-code-401
qiime
deep
uiactionsheet
yajsw
lz77
concept-insights
toolkit
atoi
wso2greg
sql-server-data-tools
keyup
nutiteq
procedural-programming
angular-gettext
appserver
template-deduction
lnk
wxformbuilder
qscrollarea
getopt
embedded-fonts
inputmismatchexception
printdialog
inappsettingskit
device-width
wcf-configuration
infobright
listactivity
unattended-processing
updatesourcetrigger
jqueryform
django-piston
tui
cuda.net
dsoframer

Resources

Encrypt Message