Android自定义LinearLayout的三种方法

方法一:在布局文件中添加自定义的LinearLayout

    <com.tang.Javen.TJDigitalClock
        android:id="@+id/digitalclock"
        android:layout_width="wrap_content"
        android:layout_height="100dp">
        
        <TextView 
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:singleLine="true"
            android:ellipsize="none"
            android:gravity="center"
            android:textSize="76sp"
            android:text="12:00"/>
        
         <TextView 
            android:id="@+id/ampm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:text="PM"/>
    </com.tang.Javen.TJDigitalClock>

然后在自定义文件中的onFinishInflate()通过findViewById找到自定义布局中的控件

public class TJDigitalClock extends LinearLayout
{
	private TextView time, date, ampm = null;
	private int timeCount = 0;
	private String timefomat;
	private Calendar mCalendar;
	private static final int CLOCK_TIME_CHANEGE = 999;
	private Context mContext = null;
	private LayoutInflater layoutInflater = null;
	public TJDigitalClock(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}
@Override
	protected void onFinishInflate()
	{
		// TODO Auto-generated method stub
		super.onFinishInflate();
		Log.d("Javen","onFinishInflate");

		time = (TextView)findViewById(R.id.time);
		ampm = (TextView)findViewById(R.id.ampm);
		date = (TextView)findViewById(R.id.date);
	}
onFinishInflate() 是当View中所有的子控件均被映射成xml后触发。


方法二:不把自定义的LinearLayout通过xml文件来布局,在布局中只放置一个LinearLayout的位置

    <LinearLayout
        android:id="@+id/facLayout"
        android:gravity="center" 
        android:background="#8602151a" 
        android:layout_width="770.0dp"
        android:layout_height="320.0dp" 
        android:layout_marginTop="30dp"/>
然后通过

facLayout = (LinearLayout) findViewById(R.id.facLayout);
来找到这个放置自定义控件的位置,最后addView加载我们需要放置的自定义控件

facLayout.addView(propertyView.getPropertyView());

propertyView.getPropertyView() = LayoutInflater.from(mContext).inflate(R.layout.propertyview, this);

这里的R.layout.propertyview就是我们自定义LinearLayout的布局了

方法三:则是完全使用代码来实现,而不使用xml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下步骤来实现: 1. 创建一个自定义LinearLayout 类,并继承 LinearLayout。 2. 在自定义 LinearLayout 类的构造方法中,调用 setWillNotDraw(false) 方法,以允许绘制自定义视图。 3. 重写 onDraw(Canvas canvas) 方法,在其中绘制矩形水印图片。可以使用 Bitmap 和 Canvas 组合来绘制图片。 4. 在 onDraw() 方法中,调用 super.onDraw(canvas) 方法,以便在绘制自定义视图之后绘制子视图。 5. 在自定义 LinearLayout 类的 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 方法中,设置 LinearLayout 的宽度和高度,以确保矩形水印图片的边角是圆形的。可以使用 MeasureSpec.getSize() 和 MeasureSpec.getMode() 方法来获取宽度和高度。 以下是示例代码: ``` public class RoundedLinearLayout extends LinearLayout { private Bitmap watermarkBitmap; public RoundedLinearLayout(Context context) { super(context); setWillNotDraw(false); } public RoundedLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); } public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setWillNotDraw(false); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (watermarkBitmap != null) { int bitmapWidth = watermarkBitmap.getWidth(); int bitmapHeight = watermarkBitmap.getHeight(); // 获取 LinearLayout 的宽度和高度 int widthSpec = MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY); int heightSpec = MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY); // 创建 Canvas 对象 Canvas watermarkCanvas = new Canvas(watermarkBitmap); // 创建矩形对象 RectF rectF = new RectF(0, 0, bitmapWidth, bitmapHeight); // 创建画笔对象 Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); // 绘制圆角矩形 watermarkCanvas.drawRoundRect(rectF, 60, 60, paint); // 在 canvas 上绘制矩形水印图片 canvas.drawBitmap(watermarkBitmap, null, new Rect(0, 0, getWidth(), getHeight()), null); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 获取 LinearLayout 的宽度和高度 int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); // 设置 LinearLayout 的宽度和高度 setMeasuredDimension(width, height); // 创建 Bitmap 对象 watermarkBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } } ``` 你可以在 XML 布局文件中使用该自定义 LinearLayout 类,如下所示: ``` <com.example.RoundedLinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 添加子视图 --> </com.example.RoundedLinearLayout> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值