android weight高度,android – 如何使用layout_weight获得相同大小的宽度和高度[方形]?...

您必须覆盖onMeasure方法设置与高度相同的高度.请看这个问题:

Simple way to do dynamic but square layout

这是我提出的完整解决方案:

SquareLinearLayout.java:

public class SquareLinearLayout extends LinearLayout {

public SquareLinearLayout(Context context) {

super(context);

}

public SquareLinearLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

public SquareLinearLayout(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int width = MeasureSpec.getSize(widthMeasureSpec);

int height = MeasureSpec.getSize(heightMeasureSpec);

int widthDesc = MeasureSpec.getMode(widthMeasureSpec);

int heightDesc = MeasureSpec.getMode(heightMeasureSpec);

int size = 0;

if (widthDesc == MeasureSpec.UNSPECIFIED

&& heightDesc == MeasureSpec.UNSPECIFIED) {

size = getContext().getResources().getDimensionPixelSize(R.dimen.default_size); // Use your own default size, for example 125dp

} else if ((widthDesc == MeasureSpec.UNSPECIFIED || heightDesc == MeasureSpec.UNSPECIFIED)

&& !(widthDesc == MeasureSpec.UNSPECIFIED && heightDesc == MeasureSpec.UNSPECIFIED)) {

//Only one of the dimensions has been specified so we choose the dimension that has a value (in the case of unspecified, the value assigned is 0)

size = width > height ? width : height;

} else {

//In all other cases both dimensions have been specified so we choose the smaller of the two

size = width > height ? height : width;

}

setMeasuredDimension(size, size);

}

}

activity_my.xml:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/listview"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin"

tools:listitem="@layout/item"

tools:context=".MyActivity">

item.xml:

android:orientation="horizontal" android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_blue_bright">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="Test 1"

android:textSize="24sp" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1"

android:background="@android:color/holo_green_light">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:text="Test 2"

android:textSize="24sp" />

MyActivity.java:

public class MyActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my);

ListView listView = (ListView) findViewById(R.id.listview);

listView.setAdapter(new BaseAdapter() {

private LayoutInflater mInflater = LayoutInflater.from(MyActivity.this);

@Override

public int getCount() {

return 100;

}

@Override

public Object getItem(int position) {

return null;

}

@Override

public long getItemId(int position) {

return 0;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null)

convertView = mInflater.inflate(R.layout.item, parent, false);

return convertView;

}

});

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值