android - 背景中的百分比
问题描述
我有点卡住了一个问题,我希望你能帮助我。我正在使用 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,但是当我以编程方式执行时,它根本不起作用。
为什么不工作?
我真的希望你能帮我解决这个问题。
非常感谢
解决方案
尝试将海拔设置为 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);
推荐阅读
- .net-4.0 - .net 4.0 上的 SAML 2.0
- linux - Unix进程之间的连接,如何获取给定用户名的全名和根目录
- ios - 将 PHLivePhotoView 添加到情节提要
- react-native - 将文本动态添加到 TextInput
- android - 工作室 SQLiteException 中的错误:json_each
- hyperledger-fabric - 在网络通信丢失后,超级账本 Fabirc 中的两个背书节点可以相互同步吗?
- git - 每次提交到 Git 时都会自动更改 npm 包的版本
- javascript - 如何全局定义套接字变量
- javascript - 如何格式化这样的数字 xxxx-xx-xxxx
- visual-studio - 在 Visual Studio 2019 中使用 v141 平台工具集构建 C++/CX 项目失败