android学习,LinearLayout

android学习,LinearLayout任何android程序,都是在一个界面里添加有数的控件,然后,再换个界面添加另一批控件,然后,可以能过改变控件来改变这个app的功能,这里有两种

大家好,欢迎来到IT知识分享网。

任何android程序,都是在一个界面里添加有数的控件,然后,再换个界面添加另一批控件,然后,可以能过改变控件来改变这个app的功能,这里有两种方式,改变控件,一种是xml文件里设置属性,一种是通过java在运行时改变这个控件状态,从而实现换页功能。

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar2" android:layout_width="match_parent" android:layout_height="50dp" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" android:theme="?attr/actionBarTheme" /> <LinearLayout android:id="@+id/blue" android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="1" android:orientation="vertical" android:background="#00f0f0"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" android:textColor="#0000ff" android:textSize="30sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"> <Button android:id="@+id/btn_green" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="changgreen" android:text="green" /> <Button android:id="@+id/btn_red" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="red" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/green" android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="0" android:background="#00ff00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" android:textColor="#0000ff" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:id="@+id/red" android:layout_width="match_parent" android:layout_height="1dp" android:layout_weight="0" android:background="#ff0000"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello world!" android:textColor="#ffff00" android:textSize="30sp" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

以上是activity_main.xml文件内容,这里就是通过布局文件来设置换件状态。

public class MainActivity extends AppCompatActivity { LinearLayout green,blue,red; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); green=findViewById(R.id.green); blue=findViewById(R.id.blue); red=findViewById(R.id.red); } public void changgreen(View view) { LinearLayout.LayoutParams params1=(LinearLayout.LayoutParams) green.getLayoutParams(); params1.weight=1.0f; green.setLayoutParams(params1); LinearLayout.LayoutParams params2=(LinearLayout.LayoutParams) blue.getLayoutParams(); params2.weight=0.0f; blue.setLayoutParams(params2); LinearLayout.LayoutParams params3=(LinearLayout.LayoutParams) red.getLayoutParams(); params3.weight=0.0f; red.setLayoutParams(params3); } }

这里面。changegreen()就是在app运行的时候改变linearLayout的状态!

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/48731.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信