Android以编程方式设置不同屏幕尺寸/密度的布局

时间:2017-03-24 19:19:01

标签: java android android-layout

我目前正在创建一个Android应用,我想支持多种屏幕尺寸/密度。当我在xml中设置布局时,不同屏幕尺寸的一切看起来都很好。但是,我需要以编程方式添加行。

每当我添加行时,我似乎无法在不同的设备上看起来一致。我相信在设置高度,xml中的宽度以及适当的wrap_content和match_parent时使用dp单元可以在不同的设备之间轻松转换。

以编程方式,当我尝试设置布局高度/宽度时,我一直在尝试将预期的dp值转换为像素值。我已经这样做了:

    public static int convertToPixels(int value) {
        Resources resources = mContext.getResources();
        int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
        return x;
    }

总体而言,高度看起来不错,但宽度看起来不太好。例如,行的宽度看起来如此,在较小的设备上,信息整齐地显示在整个行上,这就是我想要的。但是,当我尝试在平板电脑上运行应用程序时,信息只会延伸到行的一半,这看起来不太好。我想要缩小尺寸并整齐地显示,就像在较小的设备上一样。

如果有人知道我的问题可能是什么,我将不胜感激。下面是使用java添加行的源:

        LinearLayout row = new LinearLayout(mContext);
        row.setOrientation(LinearLayout.HORIZONTAL);
        row.setId(Integer.parseInt(txn.getId().toString()));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
        params.setMargins(0, convertToPixels(1), 0, 0);

        row.setLayoutParams(params);
        row.setBackgroundColor(mContext.getResources().getColor(R.color.white));

        LinearLayout imageLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
        imageParams.gravity = Gravity.CENTER;
        imageLayout.setLayoutParams(imageParams);
        ImageView image = new ImageView(mContext);


        if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
        } else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
        } else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
            image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
        }

        imageLayout.addView(image);

        row.addView(imageLayout);

        LinearLayout txnMiddleLayout = new LinearLayout(mContext);
        txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
        txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
        txnMiddleLayout.setLayoutParams(txnTopParams);

        TextView txnTopContents = new TextView(mContext);
        txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        txnTopContents.setText(txn.getTopLineContents());
        txnTopContents.setTextColor(Color.BLACK);
    //    txnTopContents.setTextSize(convertToPixels(16));
        TextView txnBottomContents = new TextView(mContext);
        txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        txnBottomContents.setText(txn.getBottomLineContents());
    //    txnBottomContents.setTextSize(convertToPixels(12));

        txnMiddleLayout.addView(txnTopContents);
        txnMiddleLayout.addView(txnBottomContents);

        row.addView(txnMiddleLayout);


        LinearLayout txnBottomLayout = new LinearLayout(mContext);
        txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
        txnBottomLayout.setLayoutParams(txnBottomParams);

        TextView amount = new TextView(mContext);
        amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        amount.setText(txn.getAmountStr());
        amount.setTextColor(Color.BLACK);
 //      amount.setTextSize(convertToPixels(16));

        TextView date = new TextView(mContext);
        date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
        date.setText(txn.getDateStr());
    //    date.setTextSize(convertToPixels(12));

        txnBottomLayout.addView(amount);
        txnBottomLayout.addView(date);

        row.addView(txnBottomLayout);

        txnList.addView(row);

1 个答案:

答案 0 :(得分:0)

我最终找到了解决方案。我没有尝试设置精确的宽度值,而是将布局或textview的宽度设置为0,而是使用layout_weight。

https://stackoverflow.com/a/3996104/4056947