本文主要是介绍Android LinearLayout使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本例通过几个Button 来展示及操作LinearLayout的使用方法,LinearLayout线性布局 排列方式有“水平”和“垂直”,最大的特点是设定好排列方式后所包含的组件会自动安照指定排列方式排列,而不用对子组件设置座标参数,简单易用适合多个组件以水平或垂直方式排列的情况。
//LinearLayoutActivity.java
package shortcut.song.com.myapplication;import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.Button;public class LinearLayoutActivity extends AppCompatActivity {LinearLayout mLinearLayout;Button mButtonH;Button mButtonV;Button mButtonLeft;Button mButtonRight;Button mButtonTop;Button mButtonBottom;Button mButtonCenter;@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_linear_layout);mLinearLayout = (LinearLayout)findViewById(R.id.activity_linear_layout);mButtonH = (Button)findViewById(R.id.btn_horzontal);mButtonV = (Button)findViewById(R.id.btn_vertical);mButtonLeft = (Button)findViewById(R.id.linear_left);mButtonRight = (Button)findViewById(R.id.linear_right);mButtonTop = (Button)findViewById(R.id.linear_top);mButtonBottom = (Button)findViewById(R.id.linear_bottom);mButtonCenter = (Button)findViewById(R.id.linear_center);mButtonH.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);//水平}});mButtonV.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setOrientation(LinearLayout.VERTICAL);//垂直}});mButtonLeft.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setGravity(Gravity.LEFT);//靠左}});mButtonRight.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setGravity(Gravity.RIGHT);//靠右}});mButtonTop.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setGravity(Gravity.TOP);//顶部}});mButtonBottom.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setGravity(Gravity.BOTTOM);//底部}});mButtonCenter.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {mLinearLayout.setGravity(Gravity.CENTER);//居中}});} }
//Layout布局文件activity_linear_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_linear_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context="shortcut.song.com.myapplication.LinearLayoutActivity"><Button android:id="@+id/btn_horzontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Horzontal" /><Button android:id="@+id/btn_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Vertical" /><Button android:id="@+id/linear_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left" /><Button android:id="@+id/linear_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Right" /><Button android:id="@+id/linear_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top" /><Button android:id="@+id/linear_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bottom" /><Button android:id="@+id/linear_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Center" /> </LinearLayout>
这篇关于Android LinearLayout使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!