Reputation: 38
I want to space out 3 TextViews across the width of the screen, with the 1st TextView touching the left margin, and the 3rd TextView touching the right margin.
Currently I'm able to achieve the following:
|text[space]text[space]text[space]|
and
|[space]text[space]text[space]text|
and
|[space/2]text[space]text[space]text[space/2]|
By setting left, right, and center gravities, respectively, within a horizontal ListView.
I want to achieve:
|text[space]text[space]text|
But I can't seem to get it. Here's my XML:
<LinearLayout
android:id="@+id/graph_xaxis_labels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linegraph_fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="horizontal">
<TextView
android:id="@+id/textview_xaxis_0"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="8sp"
android:text="02/12"
/>
<TextView
android:id="@+id/textview_xaxis_1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="8sp"
android:text="02/13"
/>
<TextView
android:id="@+id/textview_xaxis_2"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="8sp"
android:text="02/14"
/>
</LinearLayout>
Any help will be greatly appreciated.
PS: I know hardcoding TextView text is bad. That's only temporary.
Upvotes: 2
Views: 4849
Reputation: 332
Maybe this is not exactly what you are looking for, but if you want to add items programatically and dynamically, here is the sample code github
Upvotes: 0
Reputation: 27236
You're almost there. You're missing the Gravity.
UPDATE: To center more than three, you need to change the layout… updated below for 6 labels: (The hint was given by Haresh answer) It has a few drawbacks but should do the trick.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/graph_xaxis_labels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textview_xaxis_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/12"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/13"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/14"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/15"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/16"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/17"/>
</LinearLayout>
Upvotes: 5
Reputation: 6096
you are missing gravity for 2 nd 3rd textview ..try this way
<TextView
android:id="@+id/textview_xaxis_0"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="8sp"
android:text="02/12"
/>
<TextView
android:id="@+id/textview_xaxis_1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:textSize="8sp"
android:text="02/13"
/>
<TextView
android:id="@+id/textview_xaxis_2"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="8sp"
android:text="02/14"
/>
</LinearLayout>
Upvotes: 0
Reputation: 24848
// try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/graph_xaxis_labels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/linegraph_fragment"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textview_xaxis_0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/12"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/13"/>
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"/>
<TextView
android:id="@+id/textview_xaxis_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="8sp"
android:text="02/14"/>
</LinearLayout>
Upvotes: 0