制作一个布局大作为真正的内容,而不是背景 [英] Making a layout big as the real content, not the background

查看:130
本文介绍了制作一个布局大作为真正的内容,而不是背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个孩子的RelativeLayout:二textviews,一是低于其他。在 RelativeLayout的 WRAP_CONTENT 随着高度的规则。

现在这个 relative_layout 有一个背景下,为 800x500 键,因此需要这个 500 高像素作为其高度。我不得不裁剪,让像 150dp 作为一个高度的规则,以适应两textviews不会浪费比需要的方式更多的空间。

如何使用这种背景下,但要的高度 RelativeLayout的真正的 WRAP_CONTENT

 < RelativeLayout的
        机器人:ID =@ + ID /中期
        机器人:layout_below =@ ID /顶
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =130dp/ *如果我在这里WRAP_CONTENT要花500像素身高* /
        机器人:layout_marginLeft =10dp
        机器人:layout_marginRight =10dp
        机器人:layout_marginBottom =5DP
        机器人:paddingLeft =10dp
        机器人:背景=@绘制/ counter_background2// 800x500>            <的TextView
                机器人:ID =@ + ID / CounterMechanicsTitle
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_centerHorizo​​ntal =真
                机器人:layout_marginTop =10dp
                机器人:TEXTSIZE =30dp
                机器人:文字=@字符串/技工/>            <的TextView
                机器人:ID =@ + ID / CounterMechanics
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =WRAP_CONTENT
                机器人:layout_below =@ ID / CounterMechanicsTitle
                机器人:layout_marginTop =5DP
                机器人:TEXTSIZE =20dp
                机器人:文字=@字符串/技工/>        < / RelativeLayout的>


解决方案

好吧,这里有一个方法:

  TextView的CounterMechanicsTitle =(的TextView)findViewById(R.id.C​​ounterMechanicsTitle);
    TextView的CounterMechanics =(的TextView)findViewById(R.id.C​​ounterMechanics);
    。可绘制博士= getResources()getDrawable(R.drawable.counter_background2);
    RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.mid);    INT heightOfTextViews = CounterMechanicsTitle.getHeight()+ CounterMechanics.getHeight();
    //现在你有两个TextViews的高度    位图的位图=((BitmapDrawable)博士).getBitmap();
    //缩放绘制到heightOfTextViews * 1.6×heightOfTextViews,所以要正确缩放
    可绘制D =新BitmapDrawable(getResources(),Bitmap.createScaledBitmap(位图,heightOfTextViews * 1.6,heightOfTextViews,真实));rl.setBackground(四);
    //可选
    //rl.getLayoutParams().height = heightOfTextViews;
    //rl.getLayoutParams().width = heightOfTextViews * 1.6;

看是否有此修复它。

I have a relativeLayout with two children: two textviews, one below the other. The relativeLayout has wrap_content as the height rule.

Now this relative_layout has a background that is 800x500 and so it takes this 500 height pixel as its height. I have to crop it, giving something like 150dp as a height rule to accommodate the two textviews without wasting way more space than needed.

How can I use that background, but make the height of the relativeLayout a real wrap_content?

 <RelativeLayout
        android:id="@+id/mid"
        android:layout_below="@id/top"
        android:layout_width="wrap_content"
        android:layout_height="130dp" /*if i have wrap_content here it take 500px as height */
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="5dp"
        android:paddingLeft="10dp"
        android:background="@drawable/counter_background2" // 800x500>

            <TextView
                android:id="@+id/CounterMechanicsTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="10dp"
                android:textSize="30dp"
                android:text="@string/mechanics" />

            <TextView
                android:id="@+id/CounterMechanics"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/CounterMechanicsTitle"
                android:layout_marginTop="5dp"
                android:textSize="20dp"
                android:text="@string/mechanics" />

        </RelativeLayout>

解决方案

Ok, here's one approach:

    TextView CounterMechanicsTitle = (TextView) findViewById(R.id.CounterMechanicsTitle);
    TextView CounterMechanics = (TextView) findViewById(R.id.CounterMechanics);
    Drawable dr = getResources().getDrawable(R.drawable.counter_background2);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.mid);

    int heightOfTextViews = CounterMechanicsTitle.getHeight() + CounterMechanics.getHeight();
    //Now you have the height of the two TextViews

    Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
    // Scale the drawable to heightOfTextViews *1.6 x heightOfTextViews, so to be scaled properly
    Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, heightOfTextViews*1.6, heightOfTextViews, true));

rl.setBackground(d);
    //Optional 
    //rl.getLayoutParams().height = heightOfTextViews;
    //rl.getLayoutParams().width = heightOfTextViews*1.6;

See if this fixes it.

这篇关于制作一个布局大作为真正的内容,而不是背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆