【问题标题】:Percentage in background背景中的百分比
【发布时间】:2018-07-27 06:27:06
【问题描述】:

我遇到了一个问题,希望您能帮助我。我正在使用 Android Studio 开发一个 Android 应用程序。

我有一个包含 2 个元素的 TableLayout。第一列是一个在背景中绘制百分比的 LinearLayout,第二列只是一个简单的 textview(例如,我在其中放了两行)。

我已经解决了 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,但是当我以编程方式执行时,它根本不起作用。

为什么不工作?

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

非常感谢

【问题讨论】:

    标签: android android-layout android-linearlayout background-color android-relativelayout


    【解决方案1】:

    尝试将高度设置为 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);
    

    【讨论】:

    • 嗨奥米!.. 感谢您的回答。但是 setElevation 并没有什么不同。在什么情况下我应该选择 setElevation?。
    • In what cases do I choose setElevation? 在某些情况下,父布局背景会覆盖子布局,因此为避免这种情况,您可以添加大于父布局的高度来提升子布局...
    • 谢谢 Omi .. 我会读到的。也许我需要改变实现它的方式
    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 2018-10-13
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多