一路繁花似锦绣前程
失败的越多,成功才越有价值

导航

 

安卓入门

三、布局

1、LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/ic_baseline_ac_unit_24"  :分割线
    android:dividerPadding="10dp"   :设置分隔线的padding
    android:gravity="center_vertical"   :控制组件所包含的子元素的对齐方式,可多个组合
    android:orientation="vertical"  :布局中组件的排列方式
    android:showDividers="middle"   :设置分隔线所在的位置,none(无),beginning(开始),end(结束),middle(每两个组件间)
    >

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center" :控制该组件在父容器里的对齐方式
        android:layout_weight="1"   :(权重)该属性是用来等比例的划分区域
        android:background="#ffff0000"  :为该组件设置一个背景图片,或者是直接用颜色覆盖
    />

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff00ff00" />

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#ff0000ff" />

</LinearLayout>
2、RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <RelativeLayout
        android:id="@+id/layout1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true" :对齐父容器底部
        android:layout_centerHorizontal="true"  :中间水平方向
        android:background="@color/red" 
    />

    <RelativeLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_above="@id/layout1"  :参照布局的上方
        android:layout_margin="10dp"
        android:layout_toRightOf="@id/layout1"  :参照布局的右边
        android:background="@color/green" 
    />

</RelativeLayout>
3、FrameLayout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/red"
        android:foreground="@drawable/ic_baseline_ac_unit_24"   :设置前景
        android:foregroundGravity="right|bottom"    :设置前景位置
    />

</FrameLayout>
4、TableLayout
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:collapseColumns="0" :设置需要被隐藏的列的序号,从0开始
    android:shrinkColumns="1"   :设置允许被拉伸的列的列序号,从0开始
    android:stretchColumns="1"  :设置允许被收缩的列的列序号,从0开始
    >

    <TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"   :显示在第几列
            android:layout_span="2" :横向跨几列
            android:background="@color/green"
            android:text="单元格" />
    </TableRow>
</TableLayout>
5、GridLayout
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2" :设置行的显示个数
    android:orientation="vertical"  :设置水平显示还是垂直显示
    android:rowCount="2"    :设置列的显示个数
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_row="1"  :显示在第几行
        android:layout_rowSpan="2"  :横向跨几行
        android:layout_rowWeight="1"    :纵向剩余空间分配方式
        android:layout_column="1"   :显示在第几列
        android:layout_columnSpan="2"   :横向跨几列
        android:layout_columnWeight="1" :横向剩余空间分配方式
        android:layout_gravity="fill"   :在网格中的显示位置
        android:background="@color/green"
        android:text="单元格" />
</GridLayout>

四、列表

1、ListView
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!--ListView的layout_height属性值固定为match_parent-->
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  • list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

</LinearLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Integer[] data = {5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1};
        Context context = this;

        ListView listView = findViewById(R.id.lv);
        listView.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return data.length;
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            final class ViewHolder {
                TextView textView;
            }

            /**
             * 参数2:当条目View对象不在屏幕显示的时候,此对象不会立即销毁,而是作为历史缓存View对象,可重复使用。
             */
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder viewHolder;
                if (convertView == null) {
                    viewHolder = new ViewHolder();
                    /**
                     * inflate俗称打气筒,是个View对象,有三种方式将xml转成打气筒对象
                     *
                     * 方式一:View.inflate(getApplicationContext(), R.layout.list_item, null);
                     *
                     * 方式二:LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_item, null);
                     *
                     * 方式三:LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     *       inflate.inflate(R.layout.list_item, null);
                     */
                    convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
                    /**
                     * 不同的xml,可以用相同的id
                     */
                    viewHolder.textView = convertView.findViewById(R.id.tv);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ViewHolder) convertView.getTag();
                }
                viewHolder.textView.setText("林志玲" + data[position]);

                return convertView;
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("ll", position + "");
            }
        });
    }
}
2、RecyclerView
  • build.gradle(:app)
dependencies {
    // 添加Recyclerview依赖包
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
  • activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
  • recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp" />

</LinearLayout>
  • MyAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private Integer[] data;
    private Context context;

    public MyAdapter(Integer[] data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.recyclerview_item, null);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        if (position % 4 == 0) {
            holder.tv.setText("孟美岐,黄婷婷" + data[position]);
        } else {
            holder.tv.setText("鞠婧祎" + data[position]);
        }
    }

    @Override
    public int getItemCount() {
        return data.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onRecyclerItemClick(getAdapterPosition());
                    }
                }
            });
        }
    }

    private OnRecyclerItemClickListener mOnItemClickListener;

    public void setmOnItemClickListener(OnRecyclerItemClickListener listener) {
        mOnItemClickListener = listener;
    }

    public interface OnRecyclerItemClickListener {
        void onRecyclerItemClick(int position);
    }
}
  • MainActivity
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Integer[] data = {5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1, 5, 8, 3, 4, 9, 2, 7, 6, 1};

        RecyclerView recyclerView = findViewById(R.id.rv);

        // 线性布局
        // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // recyclerView.setLayoutManager(linearLayoutManager);

        // 网格布局
        // GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        // recyclerView.setLayoutManager(gridLayoutManager);

        // 瀑布流
        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        MyAdapter myAdapter = new MyAdapter(data, this);
        recyclerView.setAdapter(myAdapter);

        myAdapter.setmOnItemClickListener(new MyAdapter.OnRecyclerItemClickListener() {
            @Override
            public void onRecyclerItemClick(int position) {
                Log.e("ll", position + "");
            }
        });
    }
}
posted on 2021-11-02 10:49  一路繁花似锦绣前程  阅读(29)  评论(0编辑  收藏  举报