简体   繁体   English

Android linearlayouts没有按预期扩展

[英]Android linearlayouts not expanding as expected

I have this code below that creates a square with an image, textview and 2 image buttons. 我在下面的代码中创建了一个带有图像,textview和2个图像按钮的正方形。

It looks like this: 看起来像这样:

在此处输入图片说明

I want it so that the text is always in 1 line. 我希望它使文本始终在1行中。 The width of the square needs to expand so it includes the full text. 正方形的宽度需要扩展,因此它包括全文。 In the image, the text gets cropped. 在图像中,文本被裁剪。 The two little buttons (info and settings) needs to be in the corners of the square. 两个小按钮(信息和设置)必须位于正方形的角落。 Also there is another problem, the text is not vertically centered and it should be. 另外还有一个问题,文本不是垂直居中,而是应该居中。

I can't quite get it to look right. 我不能完全正确地看它。 Does anyone know how to fix this? 有谁知道如何解决这一问题?

Thanks 谢谢

public static LinearLayout createNodeWrapper(Activity context, Fish fish) {
    ImageView imageview = createImageView(context, fish);
    TextView nametv = createNameView(context, fish.getName());
    ImageButton info = createButton(context, R.drawable.ic_action_info);
    ImageButton settings = createButton(context, R.drawable.ic_action_settings);

    LinearLayout namewrapper = createNameWrapper(context);
    namewrapper.addView(info);
    namewrapper.addView(nametv);
    namewrapper.addView(settings);

    LinearLayout contentwrapper = createContentWrapper(context, fish);
    contentwrapper.addView(imageview);
    contentwrapper.addView(namewrapper);

    LinearLayout fullwrapper = createFullWrapper(context, fish);
    fullwrapper.addView(contentwrapper);

    return fullwrapper;
}

private static ImageView createImageView(Context context, Fish fish) {
    ImageView imageview = new ImageView(context);
    LinearLayout.LayoutParams params = fish.getImageParams();
    params.gravity = Gravity.CENTER;
    params.setMargins(20, 20, 20, 20);
    imageview.setLayoutParams(params);
    imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
    return imageview;
}

private static TextView createNameView(Context context, String name) {
    TextView nametv = new TextView(context);
    nametv.setText(name);
    nametv.setTextAppearance(context, R.style.node);
    nametv.setPadding(10, 0, 10, 10);
    nametv.setGravity(Gravity.CENTER);
    nametv.setSingleLine(true);
    LinearLayout.LayoutParams textparams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
    textparams.gravity = Gravity.CENTER;
    nametv.setLayoutParams(textparams);
    return nametv;
}

private static ImageButton createButton(Context context, int resId) {
    ImageButton imageButton = new ImageButton(context);
    imageButton.setPadding(0, 0, 0, 0);
    LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    imageparams.gravity = Gravity.CENTER;
    imageButton.setLayoutParams(imageparams);
    imageButton.setBackgroundResource(resId);
    return imageButton;
}

private static LinearLayout createNameWrapper(Context context) {
    LinearLayout wrapper = new LinearLayout(context);
    wrapper.setOrientation(LinearLayout.HORIZONTAL);
    wrapper.setPadding(0, 0, 0, 0);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    wrapper.setLayoutParams(params);
    return wrapper;
}

private static LinearLayout createContentWrapper(Context context, Fish fish) {
    LinearLayout wrapper = new LinearLayout(context);
    wrapper.setOrientation(LinearLayout.VERTICAL);
    wrapper.setPadding(3, 0, 3, 3);
    LinearLayout.LayoutParams params = fish.getParams();
    wrapper.setLayoutParams(params);
    wrapper.setMinimumWidth(MIN_NODE_WIDTH);
    return wrapper;
}

private static LinearLayout createFullWrapper(Context context, Fish fish) {
    LinearLayout wrapper = new LinearLayout(context);
    wrapper.setBackgroundResource(R.drawable.category_bg);
    wrapper.setOrientation(LinearLayout.VERTICAL);
    LinearLayout.LayoutParams params = fish.getParams();
    params.setMargins(10, 10, 10, 10);
    wrapper.setLayoutParams(params);
    return wrapper;
}

For the text, try this answer: https://stackoverflow.com/a/2142968/2629065 it will scale down the text until it fits the parameters you give it. 对于文本,请尝试以下答案: https : //stackoverflow.com/a/2142968/2629065它将按比例缩小文本,直到适合您提供的参数为止。

You padded the top, which adds to the height of the item, offsetting the gravity alignment. 您填充了顶部,从而增加了项目的高度,从而抵消了重力对齐。 Remove the top padding and use a margin, or pad the bottom. 取下顶部填充物并使用边距,或填充底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM