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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

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

3 回答

?
慕容3067478

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

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


相反,您需要調(diào)用setMeasuredDimension(根據(jù)onMeasure覆蓋要求)并為LayoutParams視圖提供一個新值,然后調(diào)用super.onMeasure。請記住,您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


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

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

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


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看起來像這樣:


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


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

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

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


但是,您可以做的是修改measureSpecs,然后使用它們調(diào)用super。您的視圖將永遠(yuǎn)不會知道它正在從其父級收到經(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);

}


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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