如何在Android中动态设置填充

时间:2014-04-18 14:26:15

标签: android padding

我有一种情况需要将填充设置为textview ,以便将置于imageview内。我已经通过设置填充动态实现了它但我坚信我所做的是一个非常糟糕的做法。请查看以下代码:

代码:

        final float scale = context.getResources().getDisplayMetrics().density;
        int padding_5dp = (int) (5 * scale + 0.5f);
        int padding_20dp = (int) (20 * scale + 0.5f);
        int padding_50dp = (int) (50 * scale + 0.5f);

        if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE
                || (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
            System.out.println("It is inside the large screen");
            // txtTitle.setLayoutParams(new
            // LayoutParams(padding_50dp,padding_50dp));
            txtTitle.setPadding(padding_20dp, padding_5dp, padding_5dp,
                    padding_5dp);

        }

        if (Academics_Activity.getRotation(getContext()).equalsIgnoreCase(
                "landscape")
                || Academics_Activity.getRotation(getContext())
                        .equalsIgnoreCase("reverse landscape")) {
            System.out.println("Landscape it is");
            if (position == 0) {
                txtTitle.setPadding(310, 40, 0, 0);
            }
            if (position == 1) {
                txtTitle.setPadding(280, 40, 0, 0);
            }
            if (position == 2) {
                txtTitle.setPadding(320, 40, 0, 0);
            }
            if (position == 3) {
                txtTitle.setPadding(250, 40, 0, 0);
            }
            if (position == 4) {
                txtTitle.setPadding(350, 40, 0, 0);
            }
        } else {

            if (position == 0) {
                txtTitle.setPadding(150, 40, 0, 0);
            }
            if (position == 1) {
                txtTitle.setPadding(130, 40, 0, 0);
            }
            if (position == 2) {
                txtTitle.setPadding(160, 40, 0, 0);
            }
            if (position == 3) {
                txtTitle.setPadding(100, 40, 0, 0);
            }
            if (position == 4) {
                txtTitle.setPadding(180, 40, 0, 0);
            }

        }
        txtTitle.setText(web[position]);
        imageView.setImageResource(imageId[position]);

这里我有一个listview,其中包含images和textview作为重复项。而令人遗憾的是,我有不同的图像,不接受正确的文字。如果您看到下面的屏幕截图,将会有一个准确的想法:

Screen Shot of Mobile (4 inch screen)

以上图片是在移动设备(4英寸)中拍摄的图片,它适用于4英寸及以上的手机,但视频在4英寸以下的手机中失真。

Screen Shot of Tab(10 inch screen)

上图是从屏幕尺寸为10英寸的标签中拍摄的。我已经使用了以下代码选项卡:

            final float scale = context.getResources().getDisplayMetrics().density;
            int padding_5dp = (int) (5 * scale + 0.5f);
            int padding_20dp = (int) (20 * scale + 0.5f);
            int padding_50dp = (int) (50 * scale + 0.5f);

            if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE
                    || (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
                System.out.println("It is inside the large screen");
                txtTitle.setPadding(padding_20dp, padding_5dp, padding_5dp,
                        padding_5dp);

            }

当我在手机中使用相同的技术时,视图会失真。

问题:

有没有办法根据屏幕尺寸左右动态设置填充?我知道这是一个特殊的情况,我需要根据imageview设置填充,但是有一种方法可以通过编程方式实现(我的意思是,有一种方法我不指定填充取决于每个位置原因只是很烦人!!!)?

先谢谢你们。

3 个答案:

答案 0 :(得分:2)

你不应该动态解决它。这是标记运行时框架的问题。我建议你将列表项分成两层:

  1. 带有九个补丁背景的TextView
  2. 带有适当图标的ImageView
  3. enter image description here

    在xml中它看起来像这样:

    <RelativeLayout
    
            android:layout_margin="20dp"
            android:layout_width="200dp"
            android:layout_height="wrap_content">
    
        <TextView
                android:layout_marginRight="50dp"
                android:layout_centerInParent="true"
                android:layout_margin="10dp"
                android:gravity="center"
                android:background="@drawable/nine_patch_background"
                android:text="Your title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
    
        <ImageView
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:src="@drawable/icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
    
    </RelativeLayout>
    

    结果一切都可以在任何具有不同屏幕的设备上运行:

    enter image description here

答案 1 :(得分:0)

您可以将维度放在dimens.xml文件中。然后,使用R.dimen.your_dimen_id从您的代码中访问它。

然后你必须将一个维度文件放入&#34;值&#34;目录,在&#34; values-sw600dp&#34;目录和&#34; values-sw720dp-land&#34;目录。如果他们还没有存在,只需在&#34; res&#34; 。目录

然后,您只需在每种情况下放置所需的尺寸。它在每个xml文件中都是这样的:

<resources>
    <dimen name="your_margin">10dip</dimen>
</resources>

答案 2 :(得分:0)

使用LinearLayout并将sumweight设置为父级(Linearlayout)并将weight设置为childern