第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)父視圖的尺寸調(diào)整Android視圖的大小

如何根據(jù)父視圖的尺寸調(diào)整Android視圖的大小

森林海 2019-11-08 14:25:02
如何根據(jù)其父布局的大小調(diào)整視圖的大小。例如,我有一個(gè)RelativeLayout可以填滿整個(gè)屏幕的,并且我想要一個(gè)子視圖(例如)ImageView占據(jù)整個(gè)高度,而寬度占整個(gè)寬度的1/2?我試圖重寫所有的onMeasure,onLayout,onSizeChanged,等我無(wú)法得到它的工作....
查看完整描述

3 回答

?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

我不知道是否有人還在讀這個(gè)線程,但是Jeff的解決方案只會(huì)使您半途而廢(按字面意思)。他的onMeasure所要做的就是在一半的父對(duì)象中顯示一半的圖像。問題在于,在之前調(diào)用super.onMeasure setMeasuredDimension會(huì)根據(jù)原始大小測(cè)量視圖中的所有子項(xiàng),然后在setMeasuredDimension調(diào)整視圖大小時(shí)將其切成兩半。


相反,您需要調(diào)用setMeasuredDimension(根據(jù)onMeasure覆蓋要求)并為L(zhǎng)ayoutParams視圖提供一個(gè)新值,然后調(diào)用super.onMeasure。請(qǐng)記住,您LayoutParams是從視圖的父類型派生的,而不是視圖的類型。


@Override 

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

   int parentWidth = MeasureSpec.getSize(widthMeasureSpec);

   int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

   this.setMeasuredDimension(parentWidth/2, parentHeight);

   this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));

   super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

我相信您唯一一次與父母有麻煩的地方就是父母LayoutParam


查看完整回答
反對(duì) 回復(fù) 2019-11-08
?
守候你守候我

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以通過創(chuàng)建自定義View并覆蓋onMeasure()方法來(lái)解決此問題。如果您始終在xml中的layout_width中使用“ fill_parent”,則傳遞給onMeasusre()方法的widthMeasureSpec參數(shù)應(yīng)包含父級(jí)的寬度。


public class MyCustomView extends TextView {


    public MyCustomView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }


    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);


        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);

        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

        this.setMeasuredDimension(parentWidth / 2, parentHeight);

    }

}   

您的XML看起來(lái)像這樣:


<LinearLayout 

    android:layout_width="match_parent"

    android:layout_height="match_parent">

    <view

        class="com.company.MyCustomView"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />

</LinearLayout>


查看完整回答
反對(duì) 回復(fù) 2019-11-08
?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊

我發(fā)現(xiàn)最好不要自己設(shè)置測(cè)量尺寸。父視圖和子視圖之間實(shí)際上需要進(jìn)行一些協(xié)商,并且您不想重寫所有這些代碼。


但是,您可以做的是修改measureSpecs,然后使用它們調(diào)用super。您的視圖將永遠(yuǎn)不會(huì)知道它正在從其父級(jí)收到經(jīng)過修改的消息,并將為您處理所有事情:


@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);

    int myWidth = (int) (parentHeight * 0.5);

    super.onMeasure(MeasureSpec.makeMeasureSpec(myWidth, MeasureSpec.EXACTLY), heightMeasureSpec);

}


查看完整回答
反對(duì) 回復(fù) 2019-11-08
  • 3 回答
  • 0 關(guān)注
  • 800 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)