Android线性布局未按预期扩展

时间:2014-12-09 06:53:57

标签: android android-layout android-linearlayout textview

我在下面的代码中创建了一个带有图像,textview和2个图像按钮的正方形。

看起来像这样:

enter image description here

我想要它,以便文本总是在1行。方块的宽度需要扩展,因此它包含全文。在图像中,文本被裁剪。两个小按钮(信息和设置)需要在广场的角落。还有另一个问题,文本不是垂直居中的,应该是。

我无法让它看起来正确。有谁知道如何解决这个问题?

由于

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;
}

1 个答案:

答案 0 :(得分:0)

对于文字,请尝试以下答案:https://stackoverflow.com/a/2142968/2629065它会缩小文字范围,直到它符合您提供的参数。

你填充了顶部,增加了物品的高度,抵消了重力对齐。移除顶部衬垫并使用边距,或填充底部。