在我的一个项目中,我想创建一个类似给定Image的布局
在图像中假设红色框是Textviews,蓝色框是EditText 行数未知。这取决于Web服务响应。我怎样才能做到这一点。
答案 0 :(得分:0)
您可以尝试使用此代码。如
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal" >
<Button
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Ok" />
<Button
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
我希望它会对你有所帮助。祝你好运!
答案 1 :(得分:0)
尝试在滚动视图内部的线性布局中动态添加视图。
试试这个代码。我知道这不是确切的解决方案。但是,希望它能给你一些想法。
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
答案 2 :(得分:0)
first inf_child.xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/inf_dynamicLl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/btnContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
android:text="OK"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
second inf_dynamic_view.xml layout which will generate row dynamically:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inner_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/firstText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/secontText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ThirdText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<EditText
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
/>
</LinearLayout>
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.inf_child, null);
}
LinearLayout dynamicLl = (LinearLayout) convertView.findViewById(R.id.inf_dynamicLl);
for (int rowCounter = 0; rowCounter < parentData.get(groupPosition).childData.get(childPosition).price.size(); rowCounter++)
createRow(dynamicLl, parentData.get(groupPosition).childData.get(childPosition).price.get(rowCounter), groupPosition);
=> create row dynamically
private void createRow(LinearLayout lView, String price, int childPosition) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View l = vi.inflate(R.layout.inf_dynamic_view, null);
final TextView tvFirst, tvSecond,tvThird;
final EditText edt1;
lView.addView(l);
lView.refreshDrawableState();
}