【问题标题】:Inflating UI from XML从 XML 膨胀 UI
【发布时间】:2012-08-07 03:58:33
【问题描述】:

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

编辑:我尝试在我的主布局文件中添加适当的标签(android:layout_marginBottom="25dip"),但间隙不会显示。请检查我的源代码并帮助我。问候

这是我的程序的源代码:

ExpenseWatchActivity.java(这是主Activity)

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);

    }
} 



ma​​in.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"

/>

【问题讨论】:

  • 如果有帮助记得采纳答案。

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


【解决方案1】:

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

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);

【讨论】:

  • 嗨,我听从了你的建议,但没有得到我想要的。根据您的帖子进行编辑后,所发生的一切是 list 和 list2 正在以一种奇怪的方式调整大小,但它们之间没有出现任何间隙(这就是我想要的......)
  • 我会调查的,请给我一些时间。
  • 刚刚检查过。按照我的方法,它工作得很好。但我又读了你的问题。你到底把marginBottom 属性放在哪里了?在linearlayout?这不起作用,因为边距适用于小部件并且不被子级继承。所以以编程方式是要走的路。有颜色问题吗?我的意思是可能是背景颜色没有让你看到变化。您确实在 setMargins() 方法中放置了值,对吗?
  • 好的,我会试试,让你知道。顺便问一下,你能把输出屏幕截图吗?
  • 早上我会把完整的包裹寄给你。
猜你喜欢
  • 2013-12-29
  • 2014-03-23
  • 1970-01-01
  • 2020-04-26
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多