2017-10-08 21 views
1

如何爲TextView和ImageButton設置重力?我想這些在水平位置和TextView應該佔據90%的空間和ImageButton 10%。如何爲Android中的視圖提供特定的空間量?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/info"/> 


</LinearLayout> 

回答

1

您可以使用佈局屬性作爲wieghtSum和佈局權重。您可以使用下面的代碼

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
android:layout_wightSum="1" > 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:layout_weight=".9" /> 

<ImageButton 
    android:id="@+id/imageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/info" 
    android:layout_weight=".1" 
    /> 

我希望可以解決您的問題..

0

嘗試this..Use android:layout_weight=".3"讓你textview寬度爲0dp這樣android:layout_width="0dp"

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight=".3" 
       android:text="TextView" /> 

      <ImageButton 
       android:id="@+id/imageButton" 
       android:layout_width="0dp" 
       android:layout_weight=".1" 
       android:layout_height="match_parent" 
       android:src="@android:drawable/btn_dropdown"/> 


     </LinearLayout> 
0

看看現在可以使用約束佈局的另一個選項。

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


    <Button 
     android:id="@+id/btnThird" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginStart="8dp" 
     android:text="Hello" 
     app:layout_constraintHorizontal_weight=".8" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toLeftOf="@+id/btnTwoThirds" /> 

    <Button 
     android:id="@+id/btnTwoThirds" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Wordl" 
     app:layout_constraintBottom_toBottomOf="@+id/btnThird" 
     app:layout_constraintHorizontal_weight="2" 
     app:layout_constraintLeft_toRightOf="@+id/btnThird" 
     app:layout_constraintRight_toRightOf="parent" /> 
</android.support.constraint.ConstraintLayout> 
相關問題