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);

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395423.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395423.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395423.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ABAP數據類型

數據類型表: 類型縮寫 類型 默認長度 允許長度 初始值 描述 C 文本型 1 Space 字符串數據,如Program D 日期型 8 8 00000000 日期數據,格式為YYYYMMDD F 浮點型 8 8 0 浮點數 I 整型 4 10 0 帶正負符號的整數 N 數值型 1 31 00……

cocos2d-x C++ 原始工程引擎運行機制解析

新建一個工程,相信感興趣的同學都想知道cocos引擎都是如何運行的 想知道是如何運行的,看懂四個文件即可 話不多說,上代碼: 1、首先解釋 AppDelegate.h 1 #ifndef _APP_DELEGATE_H_2 #define _APP_DELEGATE_H_3 4 #include "…

web高德maker動畫_Web Maker —我如何構建一個快速的離線前端游樂場

web高德maker動畫by kushagra gour由kushagra gour Web Maker —我如何構建一個快速的離線前端游樂場 (Web Maker — How I built a fast, offline front-end playground) Web Maker is a Chrome extension that gives you a blazing fast and offline front-end playground —…

時間小知識對于時間轉換可能有幫助

那么UTC與世界各地的時間應如何換算呢?它是將全世界分為24個時區,地球的東、西經各180(共360)被24個時區平分,每個時區各占15。以經度0(即本初子午線)為基準,東經730′與西經730′之間的區域為零時區;東經和西經的730′與2230′之…

JS——實現短信驗證碼的倒計時功能(沒有驗證碼,只有倒計時)

1、功能描述 當用戶想要獲取驗證碼時,就點擊 免費獲取驗證碼 ,然后開始倒計時,倒計時期間按鈕文字為剩余時間x秒,且不可按狀態,倒計時結束后,按鈕更改為點擊重新發送。 2、分析 必須用到定時器。按鈕點擊后…

華為OV小米鴻蒙,華為鴻蒙開源,小米OV們會采用嗎?

華為曾一直聲言不會進入電視市場,由此其他國產電視企業才會采用華為的可見企業是非常擔憂同業競爭關系的,而在智能手機市場,華為毫無疑問與其他國產手機企業都是競爭對手,更何況就在2019年下半年和2020年上半年華為在國內手機市場的份額超過四成直逼五成,其他國產手機企業被壓得…

第22天:如何使用OpenAI Gym和Universe構建AI游戲機器人

by Harini Janakiraman通過哈里尼賈納基拉曼 第22天:如何使用OpenAI Gym和Universe構建AI游戲機器人 (Day 22: How to build an AI Game Bot using OpenAI Gym and Universe) Let’s face it, AI is everywhere. A face-off battle is unfolding between Elon Musk…

軟件測試基礎理論(總結)

1. 軟件的三個要素:程序(實行特定功能的代碼) 文檔(支持代碼運行) 數據(支持程序運行一切有關) 2. 軟件的產品質量 指的是? 1)質量是指實體特性…

android studio 7200u,#本站首曬# 多圖殺貓 華為MateBook X上手體驗

#本站首曬# 多圖殺貓 華為MateBook X上手體驗2017-06-09 18:45:4437點贊33收藏78評論前幾天華為開了個發布會,帶來了三款筆記本電腦,有幸在第一時間借到了MateBook X,現在就來來做一個簡單的上手,稍晚一些再跟大家詳細聊聊使用起來…

svn強制解鎖的幾種做法

標簽: svn強制解鎖2013-12-16 17:40 12953人閱讀 評論(0) 收藏 舉報分類:SoftwareProject(23) 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 作者:朱金燦 來源:http://blog.…

數據結構和算法練習網站_視頻和練習介紹了10種常見數據結構

數據結構和算法練習網站“Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” — Linus Torvalds, creator of Linux“糟糕的程序員擔心代碼。 好的程序員擔心數據結構及其關系。” — Linux的創建者Linus Torva…

突然討厭做前端,討厭代碼_有關互聯網用戶最討厭的廣告類型的新數據

突然討厭做前端,討厭代碼You know that feeling when you’re scrolling through a blog post and then — BAM! — one of those “Sign up for our newsletter” modals pops up?當您滾動瀏覽博客文章,然后-BAM時,您就會知道這種感覺。 -彈出“注冊我…

iOS設計模式-生成器

定義&#xff1a;將一個產品的內部表象與產品的生成過程分割開來&#xff0c;從而可以使一個建造過程生成具有不同的內部表象的產品對象。 類型&#xff1a;對象創建 類圖&#xff1a; #import <Foundation/Foundation.h> interface Character : NSObject property(nonat…

《Android 應用案例開發大全(第二版)》——導讀

本節書摘來自異步社區《Android 應用案例開發大全&#xff08;第二版&#xff09;》一書中的目錄 &#xff0c;作者 吳亞峰 , 于復興 , 杜化美&#xff0c;更多章節內容可以訪問云棲社區“異步社區”公眾號查看 目 錄 第1章 初識廬山真面目——Android簡介 1.1 Android的誕生 1…

模塊--sys模塊

sys模塊是與python解釋器交互的一個接口 import sys sys.path #python解釋器找模塊的環境變量import sys print(sys.path)結果:[H:\\王文靜\\python\\4練習\\課堂練習, H:\\王文靜\\python, C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python36\\pyth…

匿名方法

與前面的可空類型是一樣的&#xff0c;匿名方法也是C# 2.0里面提出來的。 1 匿名方法 1.1 什么是匿名方法&#xff1f; 顧名思義&#xff0c;就是沒有名稱的方法&#xff0c;因為沒有名稱&#xff0c;匿名方法只能在函數定義&#xff08;匿名方法是把方法的實現和定義嵌套在了一…

使用React,Redux和Router進行真正的集成測試

by Marcelo Lotif通過馬塞洛洛蒂夫(Marcelo Lotif) 使用React&#xff0c;Redux和Router進行真正的集成測試 (Real integration tests with React, Redux and Router) After being bitten a couple of times by bad refactoring and a broken app?—?even with all my tests…

Go語言從入門到精通 - 數據類型轉換

本節核心內容 介紹 Go語言數據類型轉換的格式介紹 數據轉換代碼示例介紹 數據轉換過程中的注意事項 本小節視頻教程和代碼&#xff1a;百度網盤 可先下載視頻和源碼到本地&#xff0c;邊看視頻邊結合源碼理解后續內容&#xff0c;邊學邊練。 Go語言數據類型轉換 Go 語言使用類型…

JNI通過線程c回調java層的函數

1、參看博客&#xff1a;http://www.jianshu.com/p/e576c7e1c403 Android JNI 篇 - JNI回調的三種方法&#xff08;精華篇&#xff09; 2、參看博客&#xff1a; JNI層線程回調Java函數關鍵點及示例 http://blog.csdn.net/fu_shuwu/article/details/41121741 3 http://blog.cs…

signature=f7a4b29b93ef2b36608792fdef7f454a,Embedding of image authentication signatures

摘要&#xff1a;A method (), an apparatus, a computer readable medium and use of said method for authenticating an audio-visual signal (), such as a digital image or video, are disclosed. A signature is derived from all image regions, including areas with …