如何在按钮之间添加一个 TextView
How to add a TextView between Buttons
我正在尝试单击按钮以动态添加带有与该按钮关联的注释的文本视图。我有一个按钮 A 和 B。我想(当我点击 A 时)在按钮 A 和 B 之间添加一个 text/TextView。
这是我的 xml 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:layout_marginTop="5dp"
android:text="B"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test...."
android:visibility="invisible"
/>
</RelativeLayout>
这是我试过的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a, b , c;
final TextView txt;
final RelativeLayout layout;
layout = (RelativeLayout) findViewById(R.id.layout);
a = (Button) findViewById(R.id.a);
b = (Button) findViewById(R.id.b);
txt = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, a.getId());
layout.addView(txt, p);
setContentView(layout);
}
});
}
}
我收到以下错误消息:指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。
视图 txt
已经添加到 XML 中的 RelativeLayout。你不需要重新添加它到布局中,你只需要设置它的可见性来显示它,即
public void onClick(View v) {
txt.setVisibility(View.VISIBLE);
}
1) 可以将RelativeLayout改为LinearLayout
2) 在 Button 之间移动 TextView 并设置可见 GONE。
3) onClick() -> 将可见性设置为 View.VISIBLE。
希望能帮到你。
如果你想在按钮 A 和 B 之间放置文本视图
Xml 就像
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test...."
android:visibility="invisible"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:layout_marginTop="5dp"
android:text="B"
/>
</RelativeLayout>
并将您的 java 代码更改为
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a, b , c;
final TextView txt;
final RelativeLayout layout;
layout = (RelativeLayout) findViewById(R.id.layout);
a = (Button) findViewById(R.id.a);
b = (Button) findViewById(R.id.b);
txt = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txt.setVisibility(View.VISIBLE);
}
});
}
}
你可以试试
TextView textView;
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, a.getId());
textView = new TextView(MainActivity.this);
layout.addView(textView, p);
}
});
你可以在上面设置任何文本。
试一试
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:text="This is a test...."
android:visibility="gone"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:layout_marginTop="5dp"
android:text="B"
/>
在你的代码中
TextView textView = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});
我正在尝试单击按钮以动态添加带有与该按钮关联的注释的文本视图。我有一个按钮 A 和 B。我想(当我点击 A 时)在按钮 A 和 B 之间添加一个 text/TextView。
这是我的 xml 文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:layout_marginTop="5dp"
android:text="B"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test...."
android:visibility="invisible"
/>
</RelativeLayout>
这是我试过的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a, b , c;
final TextView txt;
final RelativeLayout layout;
layout = (RelativeLayout) findViewById(R.id.layout);
a = (Button) findViewById(R.id.a);
b = (Button) findViewById(R.id.b);
txt = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, a.getId());
layout.addView(txt, p);
setContentView(layout);
}
});
}
}
我收到以下错误消息:指定的 child 已经有一个 parent。您必须先在 child 的 parent 上调用 removeView()。
视图 txt
已经添加到 XML 中的 RelativeLayout。你不需要重新添加它到布局中,你只需要设置它的可见性来显示它,即
public void onClick(View v) {
txt.setVisibility(View.VISIBLE);
}
1) 可以将RelativeLayout改为LinearLayout
2) 在 Button 之间移动 TextView 并设置可见 GONE。
3) onClick() -> 将可见性设置为 View.VISIBLE。
希望能帮到你。
如果你想在按钮 A 和 B 之间放置文本视图
Xml 就像
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a test...."
android:visibility="invisible"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:layout_marginTop="5dp"
android:text="B"
/>
</RelativeLayout>
并将您的 java 代码更改为
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button a, b , c;
final TextView txt;
final RelativeLayout layout;
layout = (RelativeLayout) findViewById(R.id.layout);
a = (Button) findViewById(R.id.a);
b = (Button) findViewById(R.id.b);
txt = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
txt.setVisibility(View.VISIBLE);
}
});
}
}
你可以试试
TextView textView;
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, a.getId());
textView = new TextView(MainActivity.this);
layout.addView(textView, p);
}
});
你可以在上面设置任何文本。
试一试
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="A"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/a"
android:text="This is a test...."
android:visibility="gone"
/>
<Button
android:id="@+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt"
android:layout_marginTop="5dp"
android:text="B"
/>
在你的代码中
TextView textView = (TextView) findViewById(R.id.txt);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setVisibility(View.VISIBLE);
}
});