以下是我想要实现的目标。
每个圆角视图都是LinearLayout。 LinearLayout内部的3个视图应该具有1的权重。现在,当我运行以下代码时,不会显示任何内容。只显示背景但没有显示视图。
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
params1.weight = 1;
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(0, 75);
params1.weight = 1;
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
params1.weight = 1;
ll.addView(levelTV, params4);
ll.addView(recordTV, params2);
ll.addView(image, params3);
linearlayout.addView(ll, params1);
设置权重时,您不应该将宽度设置为零吗?当我将宽度更改为LinearLayout.LayoutParams.MATCH_PARENT
时,我看到的只是数字(1,2,3等),但没有别的。我怎样才能得到我想要的布局?
答案 0 :(得分:2)
有很多方法可以做到这一点,这只是对你如何处理它的偏好。大部分时间都是以XML格式保持布局的创建,因为它使您的视图创建逻辑与应用程序逻辑分离,从而使代码更清晰。此外,一旦你习惯了XML,我认为这是最简单的。我将展示几种方法来解决您的问题......
border.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke android:width="3dp"
android:color="#ff000000"
/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
/>
<corners android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>
row_view.xml - 不完美,但很容易修改。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:layout_margin="2dp" >
<TextView
android:id="@+id/text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignParentLeft="true" />
<TextView
android:id="@+id/text_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record: ###"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_centerInParent="true" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="8dp"
android:checked="true"/>
</RelativeLayout>
从这里开始,您可以通过几种不同的方式进行操作 - 第一种方法是在另一种XML中使用include语句,然后使用该布局,然后遍历所有视图以设置视图。这有点脏,所以我不是很喜欢它,但XML看起来像:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/row_view"/>
<include layout="@layout/row_view"/>
<include layout="@layout/row_view"/>
</LinearLayout>
然后你可以在你的活动中夸大这个XML,并遍历你的观点。另一种选择是膨胀和添加。这是每个的快速示例,只需更改current_type字段以查看每个...的结果:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int NUMBER_OF_ROWS = 3;
private static final int XML_TRAVERSAL = 1;
private static final int ADD_VIEW = 2;
private static int current_type = ADD_VIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(current_type == XML_TRAVERSAL){
setUpUsingTraversal();
} else {
setUpByInflating();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void setUpUsingTraversal(){
setContentView(R.layout.container_view);
LinearLayout main_view = (LinearLayout) findViewById(R.id.main_view);
int count = main_view.getChildCount();
for(int i = 0; i < count ; i++){
RelativeLayout layout = (RelativeLayout) main_view.getChildAt(i);
setTextOnViews(layout, i);
}
}
private void setUpByInflating() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
for(int x = 0; x < NUMBER_OF_ROWS ; x++){
RelativeLayout row = (RelativeLayout) getLayoutInflater().inflate(R.layout.row_view, layout, false);
setTextOnViews(row,x);
layout.addView(row);
}
setContentView(layout);
}
private void setTextOnViews(ViewGroup view, int position){
((TextView)view.getChildAt(0)).setText(Integer.toString(position));
((TextView)view.getChildAt(1)).setText("Record: " + Integer.toString(position));
}
}
最后,您可以采取的另一条路线(您提到的过于冗长)将制作一个已经包含正确值的xml文件,并且只需要充气并完成。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border" >
<TextView
android:id="@+id/text_number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text_record1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Record: ###"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"
android:paddingRight="8dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border" >
<TextView
android:id="@+id/text_number2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:text="2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text_record2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Record: ###"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"
android:paddingRight="8dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/border" >
<TextView
android:id="@+id/text_number3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text_record3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Record: ###"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="true"
android:paddingRight="8dp" />
</RelativeLayout>
</LinearLayout>
编辑:
另外,如果你想用LinearLayout而不是RelativeLayout实现这个,那么xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/border" >
<TextView
android:id="@+id/text_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/text_record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Record: ###"
android:textAppearance="?android:attr/textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:paddingRight="8dp" />
</LinearLayout>
因此,如果您以编程方式执行此操作,则应设置以下布局参数:
LinearLayout.LayoutParams forNumberAndCheckBox = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams forRecord = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1);