2017-06-03 147 views
1

在約束佈局,如果我想要的圖像是在中心水平,並在屏幕頂​​部的一定比例,那麼我可以做到這一點使圖像尺寸是從屏幕尺寸

<android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp"> 
     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/image" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      android:id="@+id/imageView" /> 
</android.support.constraint.ConstraintLayout> 

如何我可以將圖像寬度和高度設置爲屏幕寬度和高度的25%嗎?這是可行的嗎?

謝謝

回答

0

更新:

ConstrainLayout 1.1.0它已經實現了一個好得多的辦法做到這一點!

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ffff0000" 

     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 

     app:layout_constraintWidth_percent=".25" 
     app:layout_constraintWidth_default="percent" 

     app:layout_constraintHeight_percent=".25" 
     app:layout_constraintHeight_default="percent" 
     /> 

</android.support.constraint.ConstraintLayout> 

或者

你可以把準則與百分比,然後將約束到的ImageView他們。 這將是很好有一個更清潔的方式做到這一點,但我認爲它仍然比嵌套的LinearLayout配重塊

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <ImageView 
     android:id="@+id/imageView" 
     app:layout_constraintLeft_toLeftOf="@id/guideline_left" 
     app:layout_constraintRight_toRightOf="@id/guideline_right" 
     app:layout_constraintTop_toTopOf="@id/guideline_top" 
     app:layout_constraintBottom_toBottomOf="@id/guideline_bottom" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:background="#ffff0000"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_left" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.33"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_right" 
     android:orientation="vertical" 
     app:layout_constraintGuide_percent="0.66"/> 


    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_top" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.33"/> 

    <android.support.constraint.Guideline 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/guideline_bottom" 
     android:orientation="horizontal" 
     app:layout_constraintGuide_percent="0.66"/> 

</android.support.constraint.ConstraintLayout> 
+0

哦,那是洙冷靜。最後他們有百分比,但這是在支持庫? – Snake

+0

確實很酷,我想我終於要和LinearLayout說再見了。它在1.1.0版本中現在處於測試階段,您可以非常輕鬆地使用它,[請閱讀此處](https://androidstudio.googleblog.com/2017/05/constraintlayout-110-beta-1-release。 html) – lelloman

+0

這裏也一樣。如果我們有百分比,我認爲現在不需要任何其他佈局,這樣我們就可以更輕鬆地支持多個屏幕。我點擊鏈接,沒有關於2行旁邊的用法的信息..所以,當它處於beta版時,我們可以在運行4.1 – Snake

0

您可以通過編程設置像這樣: -

ImageView image_view = (ImageView) findViewById(R.id.image_view); 
calulate(image_view); 



private void calulate(View v) 
{ 
    int imgWidth = this.getResources().getDisplayMetrics().widthPixels; 
    int imgHeight = this.getResources().getDisplayMetrics().heightPixels; 
    v.getLayoutParams().height = (int) (0.25*imgHeight); 
    v.getLayoutParams().width = (int) (0.25*imgWidth); 
} 
0

隨着每一個觀點要好得多,安卓提供的覆蓋方法(onMeasure),其中有2個參數(int widthMeasureSpec, int heightMeasureSpec),您可以根據您的要求返回高度寬度。 例子:如果你想要的高度=寬度是指見方大小的視圖,你可以返回的高度和寬度是這樣的:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
{ 
    super.onMeasure(heightMeasureSpec, heightMeasureSpec); 
} 

您可以創建自定義視圖這樣的: 例子: 做一個類名CustomImageView與延伸ImageView的

public class CustomImageView extends ImageView 
{ 

    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     super.onMeasure(heightMeasureSpec, heightMeasureSpec); 

     // you can set this as per percentage also. For example: 
     //"heightMeasureSpec * 50 /100" ---- the height will be 50% of the 
     //view's width. 
    } 

} 

像這樣使用這個在您的佈局:

<CustomImageView  
    android:id="@+id/imageView"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher"  
/>