简体   繁体   English

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

[英]Android programmatically setting up layout for different screen sizes/densities

I am currently creating an Android app that I want to support multiple screen sizes/densities. 我当前正在创建一个我想要支持多种屏幕尺寸/密度的Android应用。 When I set up layouts in xml, everything looks fine across different screen sizes. 当我在xml中设置布局时,在不同的屏幕尺寸下一切看起来都很好。 However, there are rows I need to add programatically. 但是,有些行需要以编程方式添加。

Whenever I add the rows, I can't seem to get things to look consistent across different devices. 每当我添加行时,我似乎都无法在不同设备上看起来一致。 I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices. 我相信,在xml中设置高度,宽度以及适当时使用wrap_content和match_parent时,使用dp单元可以使事情在不同设备之间轻松转换。

Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. 以编程方式,当我尝试设置布局高度/宽度时,我一直在尝试将预期的dp值转换为像素值。 I've done so using this: 我这样做是这样的:

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

Overall, the heights look ok, but the widths don't look good. 总体来说,这些高度看起来不错,但是宽度看起来并不好。 For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. 例如,行的宽度看起来将使得在较小的设备上,信息整齐地显示在行中,这正是我想要的。 However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. 但是,例如,当我尝试在平板电脑上运行该应用程序时,该信息仅会延伸到行的一半,这看起来并不好。 I want the sizes to scale and neatly display just like on the smaller device. 我希望尺寸可以缩放并整齐地显示,就像在较小的设备上一样。

If anyone has any idea what my problem might be, I would greatly appreciate it. 如果有人知道我的问题可能是什么,我将不胜感激。 Below is the source for adding a row using java: 以下是使用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);

I eventually found a solution. 我最终找到了解决方案。 Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead. 我没有尝试设置确切的宽度值,而是将布局或textview的宽度设置为0,并使用layout_weight代替。

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

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

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