如何在android中设置按钮中心的图像?

时间:2014-05-14 05:11:40

标签: android button layout

我想在按钮上显示图像像这样 enter image description here

但按钮已设置:

enter image description here

我正在为此编写代码

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5" >

    <Button
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/menu"
    android:gravity="center"  />

    <TextView

        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:gravity="center"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

     <Button
        android:id="@+id/btnAction"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/icon_anchor"
        android:gravity="center" 
         />  
    </LinearLayout>

请帮我说明如何设置......

4 个答案:

答案 0 :(得分:1)

我认为硬编码你的尺寸不是一个好的观点

试试这段代码

<LinearLayout 
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="wrap_content"

android:orientation="horizontal"
android:weightSum="5" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/menu"
android:gravity="center"  />

<TextView

    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:gravity="center"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

 <Button
    android:id="@+id/btnAction"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/icon_anchor"
    android:gravity="center" 
     />  
</LinearLayout>

答案 1 :(得分:0)

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5" >

    <Button
        android:id="@+id/btnMenu"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/menu"
        android:gravity="center"  />

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

     <Button
        android:id="@+id/btnAction"
        android:layout_width="54dp"
        android:layout_height="54dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/icon_anchor"
        android:gravity="center" 
         />  
    </LinearLayout>

试试这个

答案 2 :(得分:0)

试试这个..

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/menu"
        android:gravity="center" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/icon_anchor"
        android:gravity="center" />
</LinearLayout>

答案 3 :(得分:0)

使用wrap_content作为身高..

<Button
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="@drawable/menu"
android:gravity="center"  />

<TextView

    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:gravity="center"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textStyle="bold" />

 <Button
    android:id="@+id/btnAction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="@drawable/icon_anchor"
    android:gravity="center" 
     />  
</LinearLayout>