Domanda

I'm trying to make an Activity with an NumberPicker and a TextView and I want that the textView will be next to the NumberPicker and it will be at the middle of NumberPicker Height (the TV going to be something like "choose a number:") any thing I've tried didn't worked. (I even tried to put any one in a different LinearLayout and the two LinearLayouts in a RelativeLayout and messed with the prefences and it still didn't work)

here is what I tried:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentEnd="true"
       android:paddingTop="30dp"
       android:text="@string/Main"/>

    <NumberPicker
       android:id="@+id/numberPicker1"
       android:layout_width="30dp"
       android:layout_height="wrap_content"
       android:layout_alignParentTop="true"
       android:layout_toStartOf="@+id/textView1"
       android:clickable="false"
       android:paddingTop="0dp" />

</RelativeLayout>

Thanks!

È stato utile?

Soluzione

I generally do the following for this kind of situations:

<TextView
    ...
    android:layout_alignTop="@+id/numberPicker1"
    android:layout_alignBottom="@+id/numberPicker1"
    android:gravity="center_vertical"
/>

This way the tex view appears exactly in the middle of number picker. By the way just to remind, you should put number picker before text view if you want to try this method.

Good luck!

Altri suggerimenti

You could look into a LinearLayout with a horizontal orientation, and set gravity to center to center it's children.

<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:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:orientation="horizontal"
   android:gravity="center"
   tools:context=".MainActivity" >

    <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/Main"/>

    <NumberPicker
       android:id="@+id/numberPicker1"
       android:layout_width="30dp"
       android:layout_height="wrap_content"
       android:clickable="false" />

</RelativeLayout>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top