开发者

Android Wrap TextView to width of ImageButton

I have a LinearLayout which contains an ImageButton, and underneath that, a TextView as a label. I would like to have the TextView to be centered, and to have it wrapped to the width of the ImageButton it is underneath.

EDIT: I should probably put the code I'm currently using. Here's the LinearLayout with Button and TextView

        <LinearLayout
        android:id="@+id/contestLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageButton
            android:id="@+id/contestButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/contestsbtnimg"
            android:layout_weight="1">
        </ImageButton>
        <TextView
            android:id="@+id/contestLabel"
            style="@style/MainMenuStyle"
            android:text="@string/contests">
        </TextView>
    </LinearLayout>

And here's the relevant style declaration for the TextView:

<style name="MainMenuStyle" parent="android:style/TextAppearance">

  <item name="android:textColor">#000000</item>

  <item name="android:开发者_运维问答layout_centerHorizontal">true</item>
  <item name="android:textSize">12sp</item>
  <item name="android:singleLine">false</item>
  <item name="android:layout_width">wrap_content</item>
  <item name="android:layout_height">wrap_content</item>
</style>

What I want is for the ImageButton to be on top of the LinearLayout, with the TextView acting as a label underneath, and wrapping its text to a second line if it would be wider than the button.


You should at least try to find a solution yourself.

Or give some explanation why (and what) your own findings were.

Something like this should do the trick:

<LinearLayout android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
        >
    <ImageButton android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:background="..."/>
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_horizontal"
              android:text="...."/>

</LinearLayout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜