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

时间:2019-03-15 02:28:07

标签: java android

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

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

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

1 个答案:

答案 0 :(得分:0)

  

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

从您在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;
    }
}