Android:图像按钮图标和文本
我的导航栏布局遇到问题。我希望有一个图标,其下方的文本全部位于按钮的中心。目前,当绘制图标时,它会将文本推离页面。恢复它的唯一方法是使用 android:drawablePadding="" (来自 此处)使用负值,但看起来像这样:
我的 xml看起来像这样:
<LinearLayout android:id="@+id/navbar"
android:layout_alignParentBottom="true" android:gravity="center"
android:layout_width="fill_parent" android:background="#001"
android:layout_height="60dip" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_height="fill_parent" android:id="@+id/navhomebtn" android:enabled="false"
android:textColor="#FFF" android:background="@drawable/navbuttonselected" android:drawableTop="@drawable/homeicon"
android:drawablePadding="-40dip"
android:text="Home" android:layout_weight=".25" android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navsearchbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Search" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navfavbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Favorites" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navlistbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Loans" android:layout_weight=".25"
android:layout_width="0dip" />
</LinearLayout>
I'm having trouble with the layout of my navigation bar. I was hoping to have an icon with text underneath it all centered in the button. Currently when the icon is drawn in it pushes the text off the page. The only way to get it back is by using android:drawablePadding="" (from here) using a negative value but it looks like this:
My xml looks like this:
<LinearLayout android:id="@+id/navbar"
android:layout_alignParentBottom="true" android:gravity="center"
android:layout_width="fill_parent" android:background="#001"
android:layout_height="60dip" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_height="fill_parent" android:id="@+id/navhomebtn" android:enabled="false"
android:textColor="#FFF" android:background="@drawable/navbuttonselected" android:drawableTop="@drawable/homeicon"
android:drawablePadding="-40dip"
android:text="Home" android:layout_weight=".25" android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navsearchbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Search" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navfavbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Favorites" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="@+id/navlistbtn"
android:textColor="#FFF" android:background="@drawable/navbuttons"
android:text="Loans" android:layout_weight=".25"
android:layout_width="0dip" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的按钮看起来也可以使用包含文本视图和图像视图的相对布局。然后将relativelayouts clickable属性设置为true,将background属性设置为navbuttons可绘制。
另一种方法是使用drawableLeft/Right/Top/Bottom 属性,如下所示:
The way your buttons look you might as well use a relativelayout containing a textview and an imageview. Then set the relativelayouts clickable attribute to true and the background attribute to your navbuttons drawable.
Another way is to use the drawableLeft/Right/Top/Bottom attributes like this:
要为按钮设置文本和图像,您可以这样做:
希望,它可以帮助您!
To set text and image for button you may do like this:
Hope, it helps you!