日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 代码布局设置wrap_content,android ScrollView布局(wrap_content,最大大小)

發布時間:2023/12/9 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 代码布局设置wrap_content,android ScrollView布局(wrap_content,最大大小) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我最后編寫了自己的類,擴展了ScrollView

既然你問……這是代碼.可能不是最干凈但它做我想要的.

請注意,它期望在創建視圖時設置layout_weight,并且不應在父LinearLayout中設置weigthSum,否則你會得到有趣的東西(因為這個的權重從原始值變為0,具體取決于大小ScrollView的內容)

首先,在布局文件中,視圖聲明如下:

android:id="@+id/scroll"

android:scrollbars="vertical"

android:layout_height="0dp"

android:layout_width="fill_parent"

android:layout_weight="4"

android:background="#cc0000"

>

android:id="@+id/in_scroll_view"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:background="#0000bb"

/>

然后是小部件的代碼:

import android.content.Context;

import android.util.AttributeSet;

import android.widget.LinearLayout;

import android.widget.ScrollView;

public class ShrinkingScrollView extends ScrollView {

private float original_weight=-1;

public ShrinkingScrollView(Context context) {

super(context);

}

public ShrinkingScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

}

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

super(context, attrs, defStyle);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();

float previous_weight = params.weight;

if (original_weight == -1)

original_weight = params.weight;

if ((getChildCount()>0) && (getVisibility()!=GONE)) {

super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));

int overall_height = getChildAt(0).getMeasuredHeight();

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (getMeasuredHeight() >= overall_height) {

if (previous_weight != 0) {

params.weight=0;

params.height = overall_height;

setLayoutParams(params);

post(new Runnable() {

public void run() {

requestLayout();

}

});

}

setMeasuredDimension(getMeasuredWidth(),overall_height);

}

else if (previous_weight == 0) {

params.weight = original_weight;

params.height = 0;

setLayoutParams(params);

post(new Runnable() {

public void run() {

requestLayout();

}

});

}

}

else {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

}

}

總結

以上是生活随笔為你收集整理的android 代码布局设置wrap_content,android ScrollView布局(wrap_content,最大大小)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。