在下面的代码片段中,我如何让mClickable与mTextArea的高度相同?
public class EditSpinner extends LinearLayout {
EditText mTextArea;
ImageButton mClickable;
public EditSpinner(Context context, String[] options) {
super(context);
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
setBackgroundResource(android.R.drawable.edit_text);
setAddStatesFromChildren(true);
setPadding(0, 0, 0, 0);
mTextArea = new EditText(context);
mTextArea.setSingleLine(true);
mTextArea.setBackgroundResource(0);
addView(mTextArea, new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
mClickable = new ImageButton(context);
mClickable.setBackgroundResource(0);
mClickable.setImageDrawable(
context.getResources().getDrawable(
android.R.drawable.btn_default_small));
//Scale to mTextArea.getHeight()... except getHeight doesn't work
addView(mClickable,
new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
}
}
答案 0 :(得分:1)
有点难以准确地说出你想要的东西,但听起来你要求两件不同的东西,一般来说只有一个布局是不可能的。基本上从我的解释,你说我希望按钮与图像大小相同,然后说我不希望按钮与图像大小相同(当它太大了。
我的建议是采用一种方法来破译你想要的布局样式。也许是通过检查图像大小与当前屏幕大小。然后应用适用于该特定布局的参数类型。
通过使用XML android:layout_weight
参数可以很容易地设置两个相同的大小,并且您已经熟悉wrap_content
以用于更大的图像。虽然你可以将这两个参数一起使用,但在所有情况下都不会得到你想要的东西(再次假设我理解你的帖子)。你需要一个使用权重的布局来使视图具有相同的大小,另一个使用wrap_content来允许不同的大小。
另外,只是旁注,我真的建议使用XML进行布局,除非有一些你无法用XML实现的特定内容。
编辑:以下是您的示例。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="short" />
<Button
android:id="@+id/button_2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="medium" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="loooooooooong" />
</LinearLayout>
答案 1 :(得分:0)
是否有理由不在xml中专门设置高度,以确保它们的大小相同?喜欢你的按钮,你的edittext会有这个属性吗?
android:layout_height="20dip"