从XML中扩展UI

时间:2012-08-07 03:58:34

标签: android xml android-layout android-xml android-inflate

我有一个非常基本的程序,它显示标准的Textview和扩展的Textview。扩展的将被扩展到主布局中。现在每次我添加一个新的扩展文本视图,它看起来很好,但我也希望它们之间有一些差距。这可能意味着我必须在我的xml文件中添加边距。任何人都可以帮助我如何实现这一目标吗?

编辑:我尝试在我的主布局文件中添加适当的标签(android:layout_marginBottom =“25dip”),但差距不会显示。请检查我的源代码并帮助我。此致

以下是我的程序的源代码:

ExpenseWatchActivity.java(这是主要的活动)

package com.app.expensewatch;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
import android.view.LayoutInflater;
import android.content.Context;


public class ExpenseWatchActivity extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    LinearLayout ll = (LinearLayout)findViewById(R.id.mainlayout);

    int day,mon,yr;

    Calendar cal = Calendar.getInstance();

    day = cal.get(Calendar.DATE);
    mon = 1 + cal.get(Calendar.MONTH);
    yr = cal.get(Calendar.YEAR);

    String text = getResources().getString(R.string.hello, day , mon , yr);

    TextView tx1 = (TextView)findViewById(R.id.text1);
    tx1.setText(text);

    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    List1 list = (List1)inflater.inflate(R.layout.list1, null);
    list.setText(text);
    ll.addView(list);

    List1 list2 = (List1)inflater.inflate(R.layout.list1, null);
    list2.setText(text);
    ll.addView(list2);

    }
} 



main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 

    android:id="@+id/mainlayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

     <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />


    </LinearLayout>



list1.xml

<?xml version="1.0" encoding="utf-8"?>


<com.app.expensewatch.List1

        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/list11"
        android:background="@color/solid_red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

/>

1 个答案:

答案 0 :(得分:0)

按通货膨胀创建视图时,您必须在视图使用的setMargin()对象上调用LayoutParams

List1 list = (List1)inflater.inflate(R.layout.list1, null);
list.setText(text);
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
list.setLayoutParams(lp);
ll.addView(list);