【问题标题】:Button wrap_content not working in LinearLayout按钮 wrap_content 在 LinearLayout 中不起作用
【发布时间】:2017-11-30 04:48:26
【问题描述】:

所以我一直在尝试制作一个项目,每当我按下 LayoutInflate Button 时都会创建一个新按钮(按顺序显示数字文本)。 (我在这篇文章的底部写了一个问题和一个问题)

到目前为止我所做的是这些:

1.MainActivity java文件

->使按钮膨胀的java文件 在 buttonlayout.xml 到 Mission.xml 文件中的 Linearlayout

package com.example.a13_1_mission1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

Button btn_inflation;
LinearLayout container;
static int num=1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mission1);

    btn_inflation= (Button)findViewById(R.id.inflation);
    container = (LinearLayout)findViewById(R.id.container);

    btn_inflation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            LayoutInflater inflater = getLayoutInflater();
            Button inflatedButton = 
            (Button)inflater.inflate(R.layout.buttonlayout,null);
            inflatedButton.setText("버튼"+(num++));
            container.addView(inflatedButton);
        }
    });



  }

}

2.mission.xml

->提供线性布局(包含新按钮)和膨胀按钮 UI 的文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
    android:id="@+id/inflation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="#6999"
    android:gravity="center"
    android:padding="5dp"
    android:text="LayoutInflate"
    android:textAllCaps="false"
    android:textSize="10sp" />




    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:background="#6999"
        android:orientation="vertical">


    </LinearLayout>

3.buttonlayout.xml

-->要膨胀的按钮布局在此处输入图像描述(文本大小和其他内容)。

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp">
</Button>

问题和问题:虽然我在 buttonlayout.xml 中将宽度和高度设置为 wrap_content,但新制造的按钮没有正确调整。 这是我的情况的照片以帮助理解:

As you see in the photo, the left one is the objective and the right one is mine.

【问题讨论】:

  • 将您的线性布局宽度设置为 wrap_content 。
  • 并在你的父 LinearLayout 中设置 android:gravity="center"
  • 感谢您的意见。我试过了,但这里有几个问题: 1.graybackground 在我创建第一个按钮之前不会出现。 2.虽然我创建了几个按钮,但灰色背景只显示在布局中间。
  • 请试试我的回答。

标签: android user-interface button layout android-linearlayout


【解决方案1】:

MainActivity.java

View view = getLayoutInflater().inflate(R.layout.buttonlayout,null);
((Button)view.findViewById(R.id.button)).setText("\"버튼\"+(num++)");
container.addView(view);

按钮布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp">
    </Button>
</LinearLayout>

【讨论】:

    【解决方案2】:

    请使用以下代码更新您的 xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/inflation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="#6999"
            android:gravity="center"
            android:padding="5dp"
            android:text="LayoutInflate"
            android:textAllCaps="false"
            android:textSize="10sp" />
    
        <RelativeLayout
            android:id="@+id/containermain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="20dp"
            android:background="#6999">
    
            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="20dp"
                android:orientation="vertical"/>
    
        </RelativeLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2013-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多