因此,每当我按下LayoutInflate按钮时,我一直在尝试创建一个创建新Button(按顺序使用数字文本)的项目。 (我在这篇文章的底部写了一个问题和一个问题)
到目前为止我所做的是:
1.MainActivity java file
- >膨胀按钮的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
- >提供Linearlayout(包含新按钮)和膨胀Button 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
- &gt; Button的布局是这里的inflatedenter图像描述(文本大小和东西)。
<?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>
问题&amp;问题:虽然我在buttonlayout.xml中将width和height设置为wrap_content,但是未正确调整新制造的按钮。 这是我的情况照片,以帮助理解:
As you see in the photo, the left one is the objective and the right one is mine.
答案 0 :(得分:0)
MainActivity.java
<interactive>:14:1: error:
* Ambiguous type variable `a0' arising from a use of `print'
prevents the constraint `(Show a0)' from being solved.
Probable fix: use a type annotation to specify what `a0' should be.
These potential instances exist:
instance [safe] Show Args -- Defined in `Test.QuickCheck.Test'
instance [safe] Show Result -- Defined in `Test.QuickCheck.Test'
instance Show All -- Defined in `Data.Monoid'
...plus 35 others
...plus 22 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In a stmt of an interactive GHCi command: print it
buttonlayout.xml
View view = getLayoutInflater().inflate(R.layout.buttonlayout,null);
((Button)view.findViewById(R.id.button)).setText("\"버튼\"+(num++)");
container.addView(view);
答案 1 :(得分:0)
请使用以下代码更新您的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>