在android

时间:2016-10-04 09:39:38

标签: java android android-layout

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

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

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

2 个答案:

答案 0 :(得分: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.
    }

答案 1 :(得分:0)

您可以尝试此操作,动态添加按钮:

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