[英]making buttons in android using JAVA classes
I m noob to android, I have a simple Question.我是android的菜鸟,我有一个简单的问题。 i have TextView in a RelativeLayout, i want to add a button dynamically just below that TextView which is present in a RelativeLayout, how can i do it ?
我在RelativeLayout中有TextView,我想在RelativeLayout中存在的TextView下方动态添加一个按钮,我该怎么做? thanks !
谢谢 !
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@mipmap/background"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/container">
<android.support.v4.view.ViewPager
android:id="@+id/screen3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</android.support.v4.view.ViewPager>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:paddingTop="30dp"
android:id="@+id/act3_txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:textAlignment="center"
android:textColor="#FFFFFF"
/>
<ImageView
android:paddingTop="30dp"
android:id="@+id/cam_images"
android:layout_height="250sp"
android:layout_width="wrap_content"
android:layout_below="@+id/act3_txt1" />
<TextView
android:paddingTop="30dp"
android:id="@+id/act3_txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/cam_images"
android:textSize="35sp"
android:textAlignment="center"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:paddingBottom="20dp"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
1) create a button like this inside onCreate(). 1) 在 onCreate() 中创建一个这样的按钮。
Button myButton = new Button(this);
myButton.setText("Press Me");
2) then add it into your layout. 2)然后将其添加到您的布局中。 before it you have to add id for your layout in which you want to show the button.
在此之前,您必须为要在其中显示按钮的布局添加 id。
RelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutID);
layout.addView(myButton);
Inflate the relative layout and the existing textview first首先膨胀相对布局和现有的textview
relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
textView= (TextView) findViewById(R.id.tittle);
Prepare layout params and addRule method to put the button below the textview准备布局参数和 addRule 方法将按钮放在 textview 下方
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.tittle);
Button button=new Button(this);
button.setLayoutParams(params);
button.setText("Click Me!");
relativeLayout.addView(button,1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.