-1

这是我的项目 XML 布局

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/flowLayout"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:orientation="vertical" >

    <!-- enter code here -->
</LinearLayout>

我需要在JAVA 文件中访问它,该文件应包含 3 个文本视图中的 3 个句子...

public class MainActivity extends Activity {

private View layout;
@Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    findViewById();

        //enter code here
    }
    private void findViewById() {

    View layout = findViewById(R.id.flowLayout);

        //enter code here
    }
}
4

1 回答 1

0

要将 TextView 添加到您的布局中,请执行以下操作:

LinearLayout layout = (LinearLayout)findViewById(R.id.flowLayout); //depending on which layout is it.

然后,创建一个 TextView,将其设置为文本并将其添加到布局中:

TextView tvText = new TextView(this);
tvText.setText("your desired sentence");
layout.addView(tvText);

并为您的所有 3 执行此操作TextViews,以设置高度和宽度使用 LayoutParams(示例):

LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 2, 0);

如您所见,这也是您设置 Mergins 的方式,最后将这些参数设置为textView

tvText.setLayoutParams(layoutParams);
于 2013-04-04T07:02:31.557 回答