有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓中动态添加按钮和布局

以下是使用布局资源文件的代码:

<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
          xmlns:tools="http://schemas.安卓.com/tools"
          安卓:layout_width="match_parent"
          安卓:layout_height="match_parent"
          安卓:orientation="vertical"
          安卓:clipChildren="false"
          安卓:clipToPadding="false"
          tools:context=".MainActivity">


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

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

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

        安卓:src="@drawable/munshee_logo" />

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

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


</LinearLayout>

如何将其转换为java代码?结果应该是一排按钮水平分布在屏幕上。此外,每个按钮应根据屏幕的尺寸进行缩放。请帮我实现这个


共 (2) 个答案

  1. # 1 楼答案

    在java中的布局上添加视图和组件与xml非常相似。 简单的示例方法如下:-

    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.
        }
    
  2. # 2 楼答案

    您可以尝试动态添加按钮:

    <?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>
    

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

    编辑:

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