自定义布局好文章
来源:互联网 发布:手机淘宝卖家发货流程 编辑:程序博客网 时间:2024/06/05 15:09
http://blog.csdn.net/mchenys/article/details/50408819
http://www.cnblogs.com/angeldevil/p/3836256.html
我一个需求,一个textview后边紧跟一个imageview, 但是如果空间不够的情况,textview变为....
package com.example.myapplication;import android.content.Context;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.util.AttributeSet;import android.util.Log;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;/** * Created by on 2016/8/5. */public class mylayout extends RelativeLayout{ private static final String TAG = "mylayout"; TextView mTextView; ImageView mImageview; int mImageview_width; public mylayout(Context context) { super(context); } public mylayout(Context context, AttributeSet attrs) { super(context, attrs); } public mylayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onFinishInflate() { super.onFinishInflate(); /* mTextView = (TextView) findViewById(R.id.text); mImageview = (ImageView) findViewById(R.id.image);*/ mTextView = (TextView)getChildAt(0); mTextView.setId(11); mImageview = (ImageView)getChildAt(1); Bitmap image = ((BitmapDrawable) mImageview.getDrawable()).getBitmap(); int h = image.getHeight(); mImageview_width = image.getWidth(); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); Log.d(TAG, "onLayout: :"+this.getWidth()); Log.d(TAG,"mTextView.getWidth():"+mTextView.getWidth()); Log.d(TAG, "onLayout: mImageview_width:"+mImageview_width); if (mTextView.getWidth() + mImageview_width <= this.getWidth()) { RelativeLayout.LayoutParams labelParams = (LayoutParams) mImageview.getLayoutParams(); labelParams.removeRule(ALIGN_PARENT_RIGHT); labelParams.addRule(RelativeLayout.RIGHT_OF,11); mImageview.setLayoutParams(labelParams); } else { Log.d(TAG, "onFinishInflate:setWidth " + (this.getWidth() - mImageview_width)); mTextView.setWidth(this.getWidth() - mImageview_width); } }}
布局文件:
<?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:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myapplication.MainActivity"> <com.example.myapplication.mylayout android:layout_width="200dp" android:layout_height="wrap_content" android:background="#ffcc00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:singleLine="true" android:text="12345678" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:scaleType="center" android:src="@mipmap/ic_launcher" /> </com.example.myapplication.mylayout> <com.example.myapplication.mylayout android:layout_width="80dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="#ffcc00"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_gravity="center_vertical" android:singleLine="true" android:text="12345678" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:scaleType="center" android:src="@mipmap/ic_launcher" /> </com.example.myapplication.mylayout></LinearLayout>
0 0
- 自定义布局好文章
- 栅格系统布局--淘宝ued,好文章!
- 在布局页面“文章页面中”添加,自定义UserControl
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章!
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- 好文章
- linux学习之cut的用法
- LeetCode | Binary Tree Level Order Traversal I,II
- Depth Estimation
- 119. Pascal's Triangle II
- 如何才能成为一名卓越的硬件工程师?
- 自定义布局好文章
- 整数中1出现的次数(从1到n整数中1出现的次数)
- AOJ-399 Longest Prefix
- 使用android studio集成极光推送
- javaweb--Tomcat>>认识Tomcat
- html转pdf
- MVC,MVP 和 MVVM 的图示
- poj2406 Power Strings
- 小球的距离(每次弹起的高度为原来的一半)----京东2016研发工程师编程题(二)