首页 > 解决方案 > 背景中的百分比

问题描述

我有点卡住了一个问题,我希望你能帮助我。我正在使用 Android Studio 开发一个 Android 应用程序。

我有一个包含 2 个元素的 TableLayout。第一列是在背景中绘制百分比的线性布局,第二列只是一个简单的文本视图(例如,我在其中放置了两行)。

我已经解决了 XML 表示的问题,我需要以编程方式做同样的事情。这是我的 XML:

    <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id = "@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/orionBackground"
            android:orientation="horizontal"
            android:weightSum="100">

            <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="30"
            android:background="@color/orionRed">

            <TextView
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text=""
                android:layout_gravity="right"
                android:textColor="@color/orionWhite"/>

            </LinearLayout>

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello"
            android:layout_gravity="right"
            android:background="@color/orionBackground"
            android:textColor="@color/orionWhite"/>
        </TableRow>
        <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/orionBackground"
            android:orientation="horizontal"
            android:weightSum="100">

            <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:background="@color/orionRed">

            <TextView
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:text=""
                android:textColor="@color/orionWhite"/>

            </LinearLayout>

        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="There"
            android:layout_gravity="right"
            android:background="@color/orionBackground"
            android:textColor="@color/orionWhite"/>
    </TableRow>

</TableLayout>

我需要以编程方式做同样的事情,而且我不知道我在做什么会出错。这是我的代码:

    TableLayout table = (TableLayout)findViewById(R.id.tableLayout);
    /*TextView*/
    TextView textViewHelloWorld = new TextView(getBaseContext());
    textViewHelloWorld.setText("");
    textViewHelloWorld.setLayoutParams(
    new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
    textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
    /*Child Layout*/
    LinearLayout linearLayout = new LinearLayout(getBaseContext());
    /*Here I set the percentage of the LinearLayout to 10*/
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
        0, ViewGroup.LayoutParams.MATCH_PARENT, 10f);
    linearLayout.setLayoutParams(params1);
    linearLayout.setBackgroundColor(Color.parseColor("#A23934"));
    /*Textbox Added | I tested before the above line*/
    linearLayout.addView(textViewHelloWorld);
    /*Parent Layout*/
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,100f);
    LinearLayout parentLayout = new LinearLayout(getBaseContext());
    parentLayout.setOrientation(LinearLayout.HORIZONTAL);
    /*Finally, I added the LinearLayout into de parent LinearLayout*/
    parentLayout.addView(linearLayout,params);
    TableRow rowHeader = new TableRow(getBaseContext());
    TableRow.LayoutParams params10 = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    rowHeader.setLayoutParams(params10);
    /*Then, the linear layout is Added into de TableRow*/
    rowHeader.addView(parentLayout);
    /*And added to the row*/
    table.addView(rowHeader);

从视觉上看,这就是我需要看到的方式:

我想要完成的

在此处输入图像描述

这是一个应用程序,它将显示加密货币订单簿中的金额百分比。

我的问题是我的解决方案打印了整个背景,应该只打印背景的一部分。上图打印了由 layout_weight 属性定义的百分比 30 和 60,但是当我以编程方式执行时,它根本不起作用。

为什么不工作?

我真的希望你能帮我解决这个问题。

非常感谢

标签: androidandroid-layoutandroid-linearlayoutbackground-colorandroid-relativelayout

解决方案


尝试将海拔设置为 TextView ViewCompat.setElevation(textViewHelloWorld, 10);

所以这就像

 TextView textViewHelloWorld = new TextView(getBaseContext());
textViewHelloWorld.setText("sample text");
textViewHelloWorld.setLayoutParams(
new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));
textViewHelloWorld.setTextColor(Color.parseColor("#FFFFFF"));
ViewCompat.setElevation(textViewHelloWorld, 10);

推荐阅读