2017-09-05 130 views
-1

我有一個基本活動和主要活動,但在主要活動中,我已應用weightsum,但其工作不正常。下面是預期和電流輸出 enter image description hereLinearLayout,weightSum無法正常工作

MainActivity.java

public class MainActivity extends BaseActivity { 
    LinearLayout dynamicContent,bottonNavBar; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent); 
     bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar); 
     View wizard = getLayoutInflater().inflate(R.layout.activity_main, null); 
     dynamicContent.addView(wizard); 
} 
} 

Mainactivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
    android:weightSum="2" 
    android:layout_height="match_parent" 
    android:background="#F5F5F5" 
    tools:context="com.creativeframe.arun.pro.MainActivity"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/cbseschool" 
     android:orientation="vertical"> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="@drawable/college" 
     android:orientation="vertical"> 

     </LinearLayout> 

</LinearLayout> 

BaseActivity.java

public class BaseActivity extends AppCompatActivity { 


    RadioGroup radioGroup1; 
    RadioButton home,deals,account,settings; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_base); 
     home = (RadioButton)findViewById(R.id.homebtn); 
     deals = (RadioButton)findViewById(R.id.dealsbtn); 
     account = (RadioButton)findViewById(R.id.accountbtn); 
     settings = (RadioButton)findViewById(R.id.settingbtn); 

     home.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_home_white_24dp, 0,0); 
     deals.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_navigation_white_24dp, 0,0); 
     account.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_about, 0,0); 
     settings.setCompoundDrawablesWithIntrinsicBounds(0,R.mipmap.ic_call_white_24dp, 0,0); 

     radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1); 
     radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
     { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) 
      { 
       switch (checkedId) 
       { 
        case R.id.homebtn: 
         home.setTextColor(Color.parseColor("#FF4081")); 
         startActivity(new Intent(getBaseContext(),MainActivity.class)); 
         finish(); 
         overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 
         break; 
        case R.id.dealsbtn: 
         deals.setTextColor(Color.parseColor("#FF4081")); 
         startActivity(new Intent(getBaseContext(), location.class)); 
         finish(); 
         overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 

         break; 
        case R.id.settingbtn: 
         settings.setTextColor(Color.parseColor("#FF4081")); 
         startActivity(new Intent(getBaseContext(), contact.class)); 
         finish(); 
         overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 
         break; 
        case R.id.accountbtn: 
         account.setTextColor(Color.parseColor("#FF4081")); 
         startActivity(new Intent(getBaseContext(), about.class)); 
         finish(); 
         overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 
         break; 
        default: 
         break; 
       } 
      } 
     }); 
    } 
} 
+0

'weightSum'是**完全可選**。此外,加權大小(只有一個!在寬度或高度之間選擇)必須是**完全0dp **。此外,嵌套佈局會影響性能。 ** **負。有人沒有做功課,是吧? ;) –

+1

將每個子視圖的高度更改爲0dp – codeMagic

+0

您可以在'BaseActivity'中發佈膨脹的佈局嗎?我認爲問題在那裏,因爲你的其他xml看起來是正確的。 – codeMagic

回答

1

嘗試在你的Mainactivity以下 .xml

將您的孩子LinearLayout layout_height從match_parent更改爲0dp。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
android:weightSum="2" 
android:layout_height="match_parent" 
android:background="#F5F5F5" 
tools:context="com.creativeframe.arun.pro.MainActivity"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@drawable/cbseschool" 
    android:orientation="vertical"> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 
    android:background="@drawable/college" 
    android:orientation="vertical"> 

    </LinearLayout> 

    </LinearLayout> 

在MainActivity中設置您的佈局視圖如下。

public class MainActivity extends BaseActivity { 
LinearLayout dynamicContent,bottonNavBar; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent); 
    bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar); 

} 
} 
+2

這將是更好的解釋*你已經改變了什麼,以及*如何*它將解決問題,以便其他人可以更好地學習。 – codeMagic

+0

不,它不工作 –

+0

請嘗試編輯的答案。 –

1

對於水平分使用

android:layout_width="0dp" 

和對於垂直用途:

android:layout_height="0dp" 

實施例:

 <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="fitXY" 
       android:src="@mipmap/ic_launcher"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="fitXY" 
       android:src="@mipmap/ic_launcher"/> 
     </LinearLayout> 
    </LinearLayout>