【问题标题】:Lay a Textview over an Imageview while the Imageview uses layout_weight在 Imageview 使用 layout_weight 时,在 Imageview 上放置一个 Textview
【发布时间】:2016-12-09 22:13:10
【问题描述】:

我在一个 Tablerow 中有 4 个 Imageview。它们通过layout_weight 命令进行缩放。我现在需要写在这个 Imageviews 中。有没有可能做到这一点而不必使用alignparent 意思是使用相对布局?

这是我目前的代码:

TableRow.LayoutParams params = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
    TableRow.LayoutParams params1 = new TableRow.LayoutParams(0,ViewGroup.LayoutParams.WRAP_CONTENT,1.2f);
    TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 2.4f);
    TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3.6f);
    TableRow.LayoutParams params4 = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 4.8f);


        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(params);



        ImageView rank = new ImageView(this);
        rank.setAdjustViewBounds(true);
        rank.setLayoutParams(params1);
        rank.setImageResource(R.drawable.whitebutton);

        Layout[i] = new ImageView(this);
        Layout[i].setLayoutParams(params2);
        Layout[i].setImageResource(R.drawable.width2);
        Layout[i].setAdjustViewBounds(true);

        Layout[i+10] = new ImageView(this);
        Layout[i+10].setLayoutParams(params3);
        Layout[i+10].setImageResource(R.drawable.width3);
        Layout[i+10].setAdjustViewBounds(true);


        Layout[i+20] = new ImageView(this);
        Layout[i+20].setLayoutParams(params4);
        Layout[i+20].setImageResource(R.drawable.width4);
        Layout[i+20].setAdjustViewBounds(true);


        tableRow.addView(rank);
        tableRow.addView(Layout[i]);
        tableRow.addView(Layout[i+10]);
        tableRow.addView(Layout[i+20]);
        t.addView(tableRow);

这是我要创建的界面:

好吧,另外还有4张相同大小的图片,里面有实际的高分

【问题讨论】:

    标签: android android-layout android-relativelayout android-layout-weight


    【解决方案1】:

    使用TextView 代替ImageView

    <TextView
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:id="@+id/b1"
        android:text="hai every one !"
        android:textColor="#ffff"
        android:textSize="20sp"
        android:gravity="center"
        android:background="#EF5350"
        android:padding="20dp"
        android:drawableLeft="@mipmap/ic_launcher"
        android:drawableTop="@mipmap/ic_launcher"
        android:drawableRight="@mipmap/ic_launcher"
        android:drawableBottom="@mipmap/ic_launcher"
        android:layout_centerInParent="true"/>
    

    在这里您可以使用 Text、Background 以及 4 个可绘制对象。您可以使用 paddingLeft、paddingTop、paddingRight 和 paddingBottom 排列可绘制对象。

    使用RelativeLayout 代替ImageView。在RelativeLayout 中添加TextView

    我更喜欢第一种方法。

    【讨论】:

    • 但在这种情况下,我失去了必要的体重命令
    猜你喜欢
    • 2015-03-22
    • 2021-03-19
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多