【发布时间】: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