繁体   English   中英

无法以编程方式设置视图属性

[英]Can't set view attributes programmatically

从我的主要活动中,我试图为某些视图对象设置属性。 我从另一个片段中获取了这些视图,因此不得不使用布局充气器。

由于某些原因,未设置属性。 这是我的代码:

public class MainActivity extends AppCompatActivity implements BluetoothSerialListener, BluetoothSerialRawListener, BluetoothDeviceListDialog.OnDeviceSelectedListener {

    private static final int REQUEST_ENABLE_BLUETOOTH = 1;
    private BluetoothSerial bluetoothSerial;

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;
    private Toolbar mToolbar;
    private Button bluetoothButton;
    private TextView batteryPercent;

    private View vBatteryLevelOne;
    private View vBatteryLevelTwo;
    private View vBatteryLevelThree;

    private String batteryLevelMsg;
    private float recvBatteryLevel;
    private int convBatteryLevel;
    private float batteryMax;
    private float batteryMin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();

        bluetoothSerial = new BluetoothSerial(this, this);

        batteryLevelMsg = "%";
        batteryMax = 4.2f;
        batteryMin = 3.2f;

        //using layout inflator to get reference to views on tab1dashboard
        LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
        View v = layoutInflater.inflate(R.layout.tab_1_dashboard, null);

        vBatteryLevelOne = v.findViewById(R.id.batteryLevelOne);
        vBatteryLevelTwo = v.findViewById(R.id.batteryLevelTwo);
        vBatteryLevelThree = v.findViewById(R.id.batteryLevelThree);
        batteryPercent = v.findViewById(R.id.battery_percentage);

        vBatteryLevelOne.setVisibility(View.VISIBLE);
        vBatteryLevelTwo.setVisibility(View.VISIBLE);
        vBatteryLevelThree.setVisibility(View.VISIBLE);

        bluetoothSerial.setup();
        updateBluetoothState();
    }

由于某些原因,属性未设置。

从您在github发布的项目中,这是SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> fragments = new ArrayList<>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
        fragments.add(new Tab1Dashboard());
        fragments.add(new Tab2Settings());
    }
        ....
}

两个片段已添加到此类中。 因此,如果要更改Tab1Dashboard的可见性,则应在Tab1Dashboard类内进行修改,而不是MainActivity类内进行修改。

public class Tab1Dashboard extends BaseFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab_1_dashboard, container, false);
        // change the visibility here    
        View batteryOne = rootView.findViewById(R.id.batteryLevelOne);
        batteryOne.setVisibility(View.VISIBLE);

        return rootView;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM