androidstudio中layout怎么写代码

androidstudio中layout怎么写代码,第1张

在 Android Studio 中,可以使用 XML代码编写布局。以下是编写布局的基本步骤:

1. 打开 Android Studio,创建一个新的项目或打开一个已有项目。

2. 打开 res 目录下的 layout 文件夹,右键单击该文件夹,选择 New ->Layout resource file。

3. 在d出的窗口中,输入布局文件的名称,并选择布局类型(例如,LinearLayout、RelativeLayout、ConstraintLayout 等)。

4. 点击 OK 按钮,创建布局文件。

5. 在布局文件中,使用 XML代码编写布局。例如,以下是一个简单的 LinearLayout 布局:

```

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<TextView android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!" />

<Button android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Click Me!" />

</LinearLayout>

```

6.保存布局文件,并在 Activity 中使用 setContentView() 方法将布局文件加载到界面中。例如,以下是在 MainActivity 中加载布局文件的代码:

```

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

}

}

```

以上是 Android Studio 中使用 XML代码编写布局的基本步骤。可以根据需要添加更多的布局元素和属性,以实现更复杂的布局效果。

在线性布局LinearLayout里加入view比较简单,因为属性比较少,布局简单

示例,加入一个TextView

LinearLayout layout = (LinearLayout)findViewById(R.id.layout)

TextView tv = new TextView(this)

tv.setText("hello,world")

LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

layout.addView(tv,lp)

在相对布局中RelativeLayout中加入view,属性较多

示例,加入TextView和Button,让TextView居中,并且设置Button在TextView的下方

RelativeLayout layout

TextView tv = new TextView(this)

tv.setText("hello,world")

Button btn = new Button(this)

btn.setText("button")

tv.setId(0x011)

btn.setId(0x012)

LayoutParams tvLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

LayoutParams btnLp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

//添加布局规则,居中于父类

tvLp.addRule(RelativeLayout.CENTER_IN_PARENT,RelativeLayout.TRUE)

//添加布局规则,在tv的下方

btnLp.addRule(RelativeLayout.BELOW, tv.getId())

layout.addView(tv,tvLp)

layout.addView(btn,btnLp)

public void addRule(int verb, int anchor) 方法就是给view设定布局规则,verb是规则属性,就是xml文件中的各种属性值,anchor是依靠的view的id或者比如上面的RelativeLayout.CENTER_IN_PARENT的时候就是设置true或false

Android在xml文件中可使用include包含其他定义好的布局, 可以将多处用到的布局单独出来,然后用include包含进来,这种包含方法相当于把原来布局的一部分代码独立出来,供大家共同使用,也就相当于面向对向中的类的概念差不多。下面我们逐步讲解include的作用。

先看下我们要实现的整体界面:

一、未使用Include时

通常情况下,我们直接就能写出布局代码,下面是所使用的XML代码:

[html] view plaincopy

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<!-- 第一部分 -->

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN" />

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" One Button " />

<!-- 第二部分 -->

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN" />

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" Second Button " />

<!-- 最后的按钮 -->

<Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" Another Button " />

</LinearLayout>

这段代码理解起来一点难度没有,就是几个TextView和几个Button,下面我们用include把这段代码给分割成几个文件,并完成相同的效果;

二、使用Include时

1、先将上面代码标记有“第一部分”的,代码段分离成一个文件(sublayout1.xml);

[html] view plaincopy

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#505050"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#ff0000"

android:text="第一个BTN" />

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" One Button " />

</LinearLayout>

2、再将标记有“第二部分”的代码段,分离成第二个文件(sublayout2.xml):

[html] view plaincopy

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:background="#00ff00"

android:text="第二个BTN" />

<Button

android:id="@+id/mybutton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text=" Second Button " />

</LinearLayout>

3、主文件中使用include,将上面两个文件包含进去(activity_main.xml);

[html] view plaincopy

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<include

android:id="@+id/main1"

layout="@layout/sublayout1" />

<include

android:id="@+id/main2"

layout="@layout/sublayout2" />

<Button

android:id="@+id/another"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=" Another Button " />

</LinearLayout>

这样就实现了相同的效果,这里可以看到,include并没有其它的功能,只是把一个XML布局引入进来当做自己的布局,跟直接把引用的这段代码写在include处的效果是一样的。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11637840.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存