6

It seems some views like TextView has its own padding or margin...I mean when I'm setting textSize 16dp to textView, it takes more then 24 pixels on hdpi device screen to display this control. Maybe I'm missing something, please help

<LinearLayout
        android:id="@+id/uc_button_title_panel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/closed_red" />

        <TextView
            android:id="@+id/uc_button_title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="0dp"
            android:includeFontPadding="false"
            android:background="@null"
            android:padding="0dp"
            android:text="@string/demo_text"
            android:textColor="#325083"
            android:textSize="16dp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="@color/closed_red" />
    </LinearLayout>
Orest
  • 1,857
  • 5
  • 19
  • 27
  • Are you expecting it to be exactly 24 based on the 1.5x multiplier from here: http://developer.android.com/guide/practices/screens_support.html ? – Salil Pandit Jul 10 '12 at 20:07
  • Well yeah..I suppose if I want to get 24px text size on hdpi device, I have to set textSize to 16dp or not? – Orest Jul 10 '12 at 20:16
  • Yeah you are right, was just trying to understand the relationship. So are you saying the actual text is larger than 24px or the text + the space around it? – Salil Pandit Jul 10 '12 at 20:19
  • the text and space around it has 34 pixels height – Orest Jul 10 '12 at 20:20
  • Whats your layout look like? There is a good chance your TextView is filling its parent. or something along those lines. You're probably going to have to post your actual layout (whehter its in xml or java). Reading the TextView source code, I dont see anything other than the Font related padding. Which was discussed in this SO post: http://stackoverflow.com/questions/4768738/android-textview-remove-spacing-and-padding-on-top-and-bottom You may also want to try setting the padding manually to 0 to see if that changes it... – Salil Pandit Jul 10 '12 at 20:23
  • You really shouldn't be using px anywhere in your layout. You should always be using dp or sp. – BlackHatSamurai Jul 10 '12 at 22:43
  • That's not solving my problem. I put empty view with 1 px height to see where my textView starts and ends. – Orest Jul 11 '12 at 07:26

1 Answers1

4
  <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:orientation="vertical">
  </LinearLayout>

In android while using layouts, the default margins and padding available is this: @dimen/activity_vertical_margin or @dimen/activity_horizontal_margin. I have put an example code above.