I currently have a LinearLayout with three elements, each with a weight of one:
<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"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/happy_text"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="65sp"
android:layout_height="wrap_content"
android:maxLines="1"
tools:text="Unhappy" />
<SeekBar
android:id="@+id/happiness_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:theme="@style/happinessSeekbarTheme" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/stress_text"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="65sp"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@string/stress" />
<SeekBar
android:id="@+id/stress_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:theme="@style/stressSeekbarTheme" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:id="@+id/pain_text"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="65sp"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="@string/pain" />
<SeekBar
android:id="@+id/pain_seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:maxHeight="3dp"
android:theme="@style/painSeekbarTheme" />
</LinearLayout>
And it looks like this:
In my program, I'm programatically creating and inserting a TextView with height & width WRAP_CONTENT into the root view like so:
TextView tView = new TextView(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
tView.setLayoutParams(p);
I calculate the X & Y above the SeekBar's thumb and set the TextView's X & Y to those values.
However, when I add the TextView, I assume what the program does is add the TextView's margins to the root view and so the existing weighted childs have less space to work with, causing them to look compressed like so:
Is there any way to insert the TextViews without affecting the weights of the existing views in the layout?