LinearLayout,weightSum无法正常工作

时间:2017-09-05 15:47:11

标签: android android-linearlayout layout-inflater

我有一个基本活动和主要活动,但在主要活动中我已应用weightsum但它无法正常工作。以下是预期和当前输出 enter image description here

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;
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分: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);

}
}

答案 1 :(得分: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>