Android基础#10: Layout经典布局--layout中的控件左右平均分布,使用LinearLayout实现

    优雅的设计好你的布局-----箴言-----

内容简介:

在实际开发中,经常需要将控件进行水平平均分配,本篇介绍用LinearLayout来实现水平平均。

下面是一个典型的布局文件:

(1)水平平分,在顶部显示:

默认情况下,Layout是在父的layout的顶部。

效果图1:

代码如下:

<?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"
    >


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="左"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="中"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="右"></Button>


</LinearLayout>

 

(2)水平平分,在竖直居中显示:

只需要在layout中加上android:gravity="center_vertical",即可。

效果图2:

 

代码如下:

<?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:gravity="center_vertical"
    >


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="左"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="中"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="右"></Button>


</LinearLayout>

(3)水平平分,在竖直在底部显示:

只需要在layout中加上android:gravity="center_vertical",即可。

效果图如下:

代码如下:

<?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:gravity="bottom"
    >


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="左"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="中"></Button>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="右"></Button>


</LinearLayout>

 


说明:
android:orientation:水平还是垂直。
android:gravity:在布局中的顶部,居中,还是底部显示。

android:layout_width:控件宽度。
android:layout_height:控件高度。
android:layout_weight:控件所占权重,例如,3个全是1,说明三个控件的权重是一样的,就实现了平均分配。


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个自定义Preference布局的示例代码,包含一个Button按钮并实现了点击事件: 首先,在res/layout目录下创建一个名为custom_preference.xml的布局文件,代码如下: ```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="?android:attr/listPreferredItemHeight" android:gravity="center_vertical" android:paddingEnd="?android:attr/scrollbarSize"> <!-- Preference icon --> <ImageView android:id="@+android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="10dp" android:layout_gravity="center_vertical" android:contentDescription="@null" /> <!-- Preference title and summary --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLargeInverse" android:textColor="?android:attr/textColorPrimaryInverse" android:singleLine="true" android:ellipsize="end" android:textStyle="bold" android:paddingTop="5dp" /> <TextView android:id="@+android:id/summary" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="?android:attr/textColorSecondaryInverse" android:maxLines="3" android:ellipsize="end" android:paddingBottom="5dp" /> </LinearLayout> <!-- Custom button --> <Button android:id="@+id/custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Button" android:layout_marginEnd="10dp" android:onClick="onButtonClicked" /> </LinearLayout> ``` 其,我们添加了一个Button按钮,设置了其ID为custom_button,并在其添加了一个onClick属性,指向一个名为onButtonClicked的方法。 接着,在我们的Preference类,重写onBindViewHolder方法,以及实现onButtonClicked方法,完整代码如下: ```java public class CustomPreference extends Preference { public CustomPreference(Context context, AttributeSet attrs) { super(context, attrs); setLayoutResource(R.layout.custom_preference); } @Override public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); // Get the custom button view Button button = (Button) holder.findViewById(R.id.custom_button); // Set the click listener for the button button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something when the button is clicked Toast.makeText(getContext(), "Custom button clicked", Toast.LENGTH_SHORT).show(); } }); } // Custom button click handler public void onButtonClicked(View view) { // Do something when the button is clicked Toast.makeText(getContext(), "Custom button clicked", Toast.LENGTH_SHORT).show(); } } ``` 在该类,我们重写了onBindViewHolder方法,通过ViewHolder获取了我们自定义布局的Button,并设置了其点击事件。同时,我们还实现了一个名为onButtonClicked的方法,用于处理Button的点击事件。 最后,在我们的PreferenceActivity或PreferenceFragment,添加我们自定义的Preference: ```xml <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="Custom Preference Category"> <com.example.myapplication.CustomPreference android:key="custom_preference" android:title="Custom Preference" android:summary="This is a custom preference with a button" /> </PreferenceCategory> </PreferenceScreen> ``` 这样就完成了一个自定义Preference布局,并添加了一个Button按钮并实现了点击事件的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liranke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值