【问题标题】:LinearLayout, weightSum not working properlyLinearLayout、weightSum 无法正常工作
【发布时间】:2017-09-05 15:47:11
【问题描述】:

我有一个基本活动和主要活动,但在主要活动中我申请了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;
                }
            }
        });
    }
}

【问题讨论】:

  • weightSum 完全可选。此外,加权尺寸(只有一个!在宽度或高度之间进行选择)必须恰好为 0dp。此外,嵌套布局会影响性能。 否定。有人没有做作业,对吧? ;)
  • 将每个子视图的高度改为0dp
  • 你可以在BaseActivity 中发布你膨胀的布局吗?我认为问题就在那里,因为您的其他 xml 看起来是正确的。

标签: android android-linearlayout layout-inflater


【解决方案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>

【讨论】:

    【解决方案2】:

    试试下面的 在您的 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);
    
    }
    }
    

    【讨论】:

    • 最好解释一下什么你做了什么改变,以及如何解决这个问题,以便其他人可以更好地学习。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多