简体   繁体   English

在Android中动态添加按钮和布局

[英]Dynamically adding buttons and layout in android

Here is the code using layout resource file: 这是使用布局资源文件的代码:

<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"
          android:clipChildren="false"
          android:clipToPadding="false"
          tools:context=".MainActivity">


<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:clipChildren="false"
    android:clipToPadding="false"
    >

    <Button
        android:id="@+id/im11"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/munshee_logo"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"/>

    <Button
        android:id="@+id/im12"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"

        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im13"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im14"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />
    <Button
        android:id="@+id/im15"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />


</LinearLayout>

how do I convert this into java code? 如何将其转换为Java代码? The outcome should be one row of buttons horizontally spread across the screen. 结果应该是在屏幕上水平分布的一排按钮。 also, each button should scale itself according to the dimensions of the screen. 同样,每个按钮都应根据屏幕尺寸自行缩放。 Please help me implement this. 请帮助我实现这一点。

Adding views and components on layout in java is mostly similar to xml. 在Java中的布局上添加视图和组件与xml最为相似。 Simple Example method is below:- 简单示例方法如下:

public LinearLayout createRow() {
        LinearLayout objLinearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        objLinearLayout.setWeightSum(3);
        objLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        objLinearLayout.setLayoutParams(objLayoutParams);

        Button objButton = new Button(mContext);
        LinearLayout.LayoutParams objButonLayoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        objButonLayoutParams.weight = 1;
        objButton.setText("Add Button");
        objButton.setBackgroundColor(Color.LTGRAY);
        objButton.setTextColor(Color.BLACK);
        objButton.setLayoutParams(objButonLayoutParams);

        objLinearLayout.addView(objButton);

        /*
        * Here you can add other views like Textview,Spinner,etc
        * Every components has same method like in xml.*/


        return objLinearLayout;//This layout can display where you want.
    }

You can try this, Add buttons dynamically: 您可以尝试以下操作,动态添加按钮:

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal"
tools:context="com.xanadutec.testviews.MainActivity">

</LinearLayout>

In Java Class: 在Java类中:

HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
LinearLayout ll = new LinearLayout(this);
horizontalScrollView.addView(ll);
ll.setOrientation(LinearLayout.HORIZONTAL);

for(int i = 0; i < 20; i++) {
   Button cb = new Button(this);
   cb.setText("I'm dynamic!");
   ll.addView(cb);
 }

  this.setContentView(horizontalScrollView);

EDITED: 编辑:

ScrollView scrollView =new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(layout);
        for (int i = 0; i < 10; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            for (int j = 0; j < 3; j++) {
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText("Button " + (j + 1 + (i * 3)));
                btnTag.setId(j + 1 + (i * 3));
                row.addView(btnTag);
            }

            layout.addView(row);
        }
        super.setContentView(scrollView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM