Android布局:我应该使用什么来制定此布局

时间:2017-04-11 04:10:18

标签: android android-layout

我是Android新手,我想了解android。我只是想问一下布局。我应该用什么来制定UI布局。

存款,贷款,贷款类型,可用余额和贷款金额只是 TextView

enter image description here

2 个答案:

答案 0 :(得分:0)

RelativeLayout将是最佳解决方案。您也可以使用LinearLayout,但这需要一些不利于性能的嵌套。

答案 1 :(得分:0)

@约翰

你应该去RelativeLayout

显示RelativeLayout

周围的边框

1)在res/drawable文件夹中,创建一个新文件background_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle" >
    <!-- This is the stroke you want to define -->
    <stroke android:width="1dp" 
            android:color="@android:color/black"/>
     <solid android:color="@android:color/white" /> // inner color
 </shape>

此drawable将用于设置布局周围的矩形边框

2)然后按如下所示创建布局

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">

  <RelativeLayout
    android:id="@+id/layoutOne"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_border"> //setting square border here

    <TextView
       android:id="@+id/lblDeposite"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:text="Deposits">

    <TextView
       android:id="@+id/txtDepositeValue"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentRight="true" />

    <TextView
       android:id="@+id/lblShareCapital"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:layout_below="@+id/lblDeposite"
       android:text="Share Capital">

     <TextView
        android:id="@+id/lblAvailableBalance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Available Balance"
        android:layout_below="@+id/txtDepositeValue"
        android:layout_alignParentRight="true" />

   </RelativeLayout>

     //Similar to this layout create other two

    <RelativeLayout
      android:id="@+id/layoutTwo"
      android:layout_below="@+id/layoutOne"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/background_border">

    //Your code for inside content will go here just like the layoutOne add 
        all your views here but with different ids

    </RelativeLayout>


    <RelativeLayout
      android:id="@+id/layoutThree"
      android:layout_below="@+id/layoutTwo"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/background_border">

  //Your code for inside content will go here just like the layoutTwo add 
  all your views here but with different ids

    </RelativeLayout>

 </RelativeLayout> //Closing your parent view

3)现在,既然您希望可以在活动中点击所有视图,请为所有视图添加OnClickListener

layoutOne.setOnClickListener(this);
layoutTwo.setOnClickListener(this);
layoutThree.setOnClickListener(this);

然后覆盖onClick()方法

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.layoutOne:
        //your code for first view click
        break;
        case R.id.layoutTwo:
        //your code for second view click
        break;
        case R.id.layoutThree:
        //your code for third view click
        break;           
}