【发布时间】:2014-04-04 22:22:55
【问题描述】:
我正在尝试这样做:
我尝试制作自定义方形按钮:
public class SquareButton extends Button {
public SquareButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public SquareButton(Context context) {
super(context);
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int height = getMeasuredHeight();
setMeasuredDimension(height, height);
}
}
并将其与图像下的图标和文本一起使用,但没有成功。 Button 的背景是一个带有灰色描边和白色背景颜色的形状。还有一个图标和字符串文本。
<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:background="#f5f5f5"
android:orientation="vertical"
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=".MainMenuActivity" >
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<views.SquareButton
android:id="@+id/SquareButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
<views.SquareButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<views.SquareButton
android:id="@+id/SquareButton03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
<views.SquareButton
android:id="@+id/SquareButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<views.SquareButton
android:id="@+id/SquareButton05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
<views.SquareButton
android:id="@+id/SquareButton04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="@drawable/main_menu_button"
android:drawableTop="@drawable/statistics_icon"
android:gravity="center_horizontal"
android:text="Button"
android:textAlignment="center" />
</TableRow>
</TableLayout>
</LinearLayout>
请帮忙。我已经“搜索”了很多主题以找到此类问题的解决方案,但尚未找到。 (((
【问题讨论】:
-
“没有成功”你能解释一下发生了什么吗?毕竟你的方法看起来并没有那么错误。
-
i016.radikal.ru/1403/cf/8219e3653fca.jpg - 这就是结果。如您所见 - textView 在小尺寸屏幕上“隐藏”。还有,我怎样才能改变图标的大小?